Fix enemy

This commit is contained in:
bread disco 2023-09-25 19:43:07 +02:00
parent 11cc3d18f8
commit 3c4dd3af78
8 changed files with 415 additions and 41 deletions

View file

@ -3462,6 +3462,77 @@ SpriteRenderer:
m_WasSpriteAssigned: 1
m_MaskInteraction: 0
m_SpriteSortPoint: 0
--- !u!1 &1059143199
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1059143200}
- component: {fileID: 1059143202}
- component: {fileID: 1059143201}
m_Layer: 0
m_Name: AttackRange
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
--- !u!4 &1059143200
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1059143199}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 1276761820}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1059143201
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1059143199}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: eca066342ad29a428ab979c0f5b164ae, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!61 &1059143202
BoxCollider2D:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1059143199}
m_Enabled: 1
m_Density: 1
m_Material: {fileID: 0}
m_IsTrigger: 1
m_UsedByEffector: 0
m_UsedByComposite: 0
m_Offset: {x: 1, y: 0}
m_SpriteTilingProperty:
border: {x: 0, y: 0, z: 0, w: 0}
pivot: {x: 0, y: 0}
oldSize: {x: 0, y: 0}
newSize: {x: 0, y: 0}
adaptiveTilingThreshold: 0
drawMode: 0
adaptiveTiling: 0
m_AutoTiling: 0
serializedVersion: 2
m_Size: {x: 1, y: 0.2}
m_EdgeRadius: 0
--- !u!1 &1120810954
GameObject:
m_ObjectHideFlags: 0
@ -3789,11 +3860,11 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 8818525837413079787, guid: 7ae1a20bcf566935098d5683d247e4b0, type: 3}
propertyPath: m_LocalPosition.x
value: 5.91
value: -5.3
objectReference: {fileID: 0}
- target: {fileID: 8818525837413079787, guid: 7ae1a20bcf566935098d5683d247e4b0, type: 3}
propertyPath: m_LocalPosition.y
value: -2.51
value: -2.09
objectReference: {fileID: 0}
- target: {fileID: 8818525837413079787, guid: 7ae1a20bcf566935098d5683d247e4b0, type: 3}
propertyPath: m_LocalPosition.z
@ -3912,7 +3983,7 @@ SpriteRenderer:
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_Sprite: {fileID: -9095717837082945937, guid: 207ee8102dd4143d288186ef0be518ee, type: 3}
m_Sprite: {fileID: -9095717837082945937, guid: 0c673aba64b47ed75888d625bf374a8c, type: 3}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_FlipX: 0
m_FlipY: 0
@ -3936,6 +4007,7 @@ Transform:
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 1120955042}
- {fileID: 1059143200}
m_Father: {fileID: 0}
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
@ -3951,10 +4023,12 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 5dff4e6a8da93abdaab7b34c87d320b0, type: 3}
m_Name:
m_EditorClassIdentifier:
goLeftInit: 0
speedInit: 2
goLeftInit: 1
walkSpeed: 2
sprintSpeed: 4
AttackPhaseDuration: 1
attackPhaseDuration: 1
hitForce: 10
player: {fileID: 1466091288}
--- !u!70 &1276761823
CapsuleCollider2D:
m_ObjectHideFlags: 0
@ -4538,6 +4612,11 @@ TilemapCollider2D:
m_Offset: {x: 0, y: 0}
m_MaximumTileChangeCount: 1000
m_ExtrusionFactor: 0
--- !u!1 &1466091288 stripped
GameObject:
m_CorrespondingSourceObject: {fileID: 8818525837413079781, guid: 7ae1a20bcf566935098d5683d247e4b0, type: 3}
m_PrefabInstance: {fileID: 1181643594}
m_PrefabAsset: {fileID: 0}
--- !u!1 &1515443527
GameObject:
m_ObjectHideFlags: 0

View file

@ -0,0 +1,26 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class EnemyAttack : MonoBehaviour
{
public delegate void HitByEnemyDelegate(Collider2D coll) ;
public HitByEnemyDelegate HitByEnemy ;
void Start()
{
}
private void OnTriggerStay2D(Collider2D coll)
{
if (coll.gameObject.CompareTag("Player"))
{
HitByEnemy?.Invoke(coll);
}
gameObject.SetActive(false);
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: eca066342ad29a428ab979c0f5b164ae
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -2,46 +2,66 @@ using System;using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Serialization;
public class GreenEnemy : MonoBehaviour
{
public bool goLeftInit = false;
public float speedInit;
public float walkSpeed;
public float sprintSpeed;
private bool _isSprinting= false;
public float AttackPhaseDuration;
public float attackPhaseDuration;
private float _currentSpeed;
private GameObject _attackPhaseIndicator;
private SpriteRenderer _attackPhaseIndicatorSpriteRenderer;
private bool _isAttacking = false;
public float hitForce;
private bool _directionLeft;
private int _direction = 1;
// public float sight;
private BoxCollider2D _sightCollider;
private GameObject _attackRange;
public GameObject player;
private Rigidbody2D _playerRigidbody2D;
// Start is called before the first frame update
void Start()
{
_directionLeft = goLeftInit;
_currentSpeed = goLeftInit ? -speedInit: speedInit;
_currentSpeed = walkSpeed;
if (goLeftInit)
{
TurnAround();
}
// Attack phase color
_attackPhaseIndicator = transform.Find("AttackPhaseIndicator").GameObject();
_attackPhaseIndicatorSpriteRenderer = _attackPhaseIndicator.GetComponent<SpriteRenderer>();
// Attack range
_attackRange = transform.Find("AttackRange").GameObject();
_attackRange.GetComponent<EnemyAttack>().HitByEnemy += OnHitByEnemy;
_sightCollider = transform.GetComponent<BoxCollider2D>();
_sightCollider.offset = goLeftInit ? -_sightCollider.offset : _sightCollider.offset;
}
// Update is called once per frame
void Update()
{
transform.position = new Vector3(_currentSpeed * Time.deltaTime + transform.position.x, transform.position.y,
transform.position.z);
transform.position += Time.deltaTime*_currentSpeed*_direction*Vector3.right ;
}
void TurnAround()
{
_direction *= -1;
transform.Rotate(Vector3.up, 180);
}
IEnumerator AttackSequence()
@ -49,37 +69,25 @@ public class GreenEnemy : MonoBehaviour
_currentSpeed = 0f;
_attackPhaseIndicator.SetActive(true);
_attackPhaseIndicatorSpriteRenderer.color = Color.green;
yield return new WaitForSeconds(AttackPhaseDuration);
yield return new WaitForSeconds(attackPhaseDuration);
_attackPhaseIndicatorSpriteRenderer.color = Color.yellow;
yield return new WaitForSeconds(AttackPhaseDuration);
yield return new WaitForSeconds(attackPhaseDuration);
_attackPhaseIndicatorSpriteRenderer.color = Color.red;
yield return new WaitForSeconds(AttackPhaseDuration);
yield return new WaitForSeconds(attackPhaseDuration);
_attackPhaseIndicator.SetActive(false);
if (_isSprinting)
{
_currentSpeed = _directionLeft ? -sprintSpeed : sprintSpeed;
}
else
{
_currentSpeed = _directionLeft ? -speedInit : speedInit;
}
_attackRange.SetActive(true);
_currentSpeed = _isSprinting ? sprintSpeed : walkSpeed;
}
private void OnCollisionEnter2D(Collision2D coll)
{
// Changing direction
if (coll.gameObject.CompareTag("Wall") )
{
_directionLeft = !_directionLeft;
_currentSpeed = -_currentSpeed;
_sightCollider.offset = -_sightCollider.offset;
}
}
IEnumerator OnCollisionStay2D(Collision2D coll) {
// Changing direction
if (coll.gameObject.CompareTag("Wall") )
{
TurnAround();
}
// Attacking
if (coll.gameObject.CompareTag("Player") && !_isAttacking)
{
@ -99,7 +107,8 @@ public class GreenEnemy : MonoBehaviour
_isSprinting = true;
if (!_isAttacking)
{
_currentSpeed = _directionLeft ? -sprintSpeed : sprintSpeed;
_currentSpeed = sprintSpeed;
}
}
}
@ -112,8 +121,17 @@ public class GreenEnemy : MonoBehaviour
_isSprinting = false;
if(!_isAttacking)
{
_currentSpeed = _directionLeft ? -speedInit : speedInit;
_currentSpeed = walkSpeed;
}
}
}
public void OnHitByEnemy(Collider2D coll)
{
Debug.Log(coll.gameObject.tag);
// coll.gameObject.GetComponent<Rigidbody2D>()
// .AddForce(hitForce * _direction * Vector3.right, ForceMode2D.Impulse);
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB

View file

@ -0,0 +1,240 @@
fileFormatVersion: 2
guid: 0c673aba64b47ed75888d625bf374a8c
TextureImporter:
internalIDToNameTable:
- first:
213: -9095717837082945937
second: Capsule
externalObjects: {}
serializedVersion: 12
mipmaps:
mipMapMode: 0
enableMipMap: 0
sRGBTexture: 1
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapsPreserveCoverage: 0
alphaTestReferenceValue: 0.5
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
isReadable: 0
streamingMipmaps: 0
streamingMipmapsPriority: 0
vTOnly: 0
ignoreMasterTextureLimit: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: 1
aniso: 1
mipBias: 0
wrapU: 1
wrapV: 1
wrapW: 1
nPOTScale: 0
lightmap: 0
compressionQuality: 50
spriteMode: 2
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spritePixelsToUnits: 256
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spriteGenerateFallbackPhysicsShape: 1
alphaUsage: 1
alphaIsTransparency: 1
spriteTessellationDetail: 0
textureType: 8
textureShape: 1
singleChannelComponent: 0
flipbookRows: 1
flipbookColumns: 1
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
ignorePngGamma: 0
applyGammaDecoding: 0
cookieLightType: 1
platformSettings:
- serializedVersion: 3
buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Standalone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Android
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: iPhone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Server
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
spriteSheet:
serializedVersion: 2
sprites:
- serializedVersion: 2
name: Capsule
rect:
serializedVersion: 2
x: 0
y: 0
width: 256
height: 512
alignment: 0
pivot: {x: 0.5, y: 0.5}
border: {x: 0, y: 0, z: 0, w: 0}
outline:
- - {x: -33, y: 256}
- {x: -95, y: 223}
- {x: -128, y: 161}
- {x: -128, y: -161}
- {x: -95, y: -223}
- {x: -33, y: -256}
- {x: 33, y: -256}
- {x: 95, y: -223}
- {x: 128, y: -161}
- {x: 128, y: 161}
- {x: 95, y: 223}
- {x: 33, y: 256}
physicsShape:
- - {x: 0, y: 256}
- {x: -39, y: 249}
- {x: -75, y: 231}
- {x: -103, y: 203}
- {x: -121, y: 167}
- {x: -128, y: 128}
- {x: -128, y: -128}
- {x: -121, y: -167}
- {x: -103, y: -203}
- {x: -75, y: -231}
- {x: -39, y: -249}
- {x: 0, y: -256}
- {x: 39, y: -249}
- {x: 75, y: -231}
- {x: 103, y: -203}
- {x: 121, y: -167}
- {x: 128, y: -128}
- {x: 128, y: 128}
- {x: 121, y: 167}
- {x: 103, y: 203}
- {x: 75, y: 231}
- {x: 39, y: 249}
tessellationDetail: 0
bones: []
spriteID: f669c7f71d485c180800000000000000
internalID: -9095717837082945937
vertices: []
indices:
edges: []
weights: []
outline:
- - {x: -33, y: 256}
- {x: -95, y: 223}
- {x: -128, y: 161}
- {x: -128, y: -161}
- {x: -95, y: -223}
- {x: -33, y: -256}
- {x: 33, y: -256}
- {x: 95, y: -223}
- {x: 128, y: -161}
- {x: 128, y: 161}
- {x: 95, y: 223}
- {x: 33, y: 256}
physicsShape:
- - {x: 0, y: 256}
- {x: -39, y: 249}
- {x: -75, y: 231}
- {x: -103, y: 203}
- {x: -121, y: 167}
- {x: -128, y: 128}
- {x: -128, y: -128}
- {x: -121, y: -167}
- {x: -103, y: -203}
- {x: -75, y: -231}
- {x: -39, y: -249}
- {x: 0, y: -256}
- {x: 39, y: -249}
- {x: 75, y: -231}
- {x: 103, y: -203}
- {x: 121, y: -167}
- {x: 128, y: -128}
- {x: 128, y: 128}
- {x: 121, y: 167}
- {x: 103, y: 203}
- {x: 75, y: 231}
- {x: 39, y: 249}
bones: []
spriteID: 5e97eb03825dee720800000000000000
internalID: 0
vertices: []
indices:
edges: []
weights: []
secondaryTextures: []
nameFileIdTable:
Capsule: -9095717837082945937
spritePackingTag:
pSDRemoveMatte: 0
pSDShowRemoveMatteOption: 0
userData:
assetBundleName:
assetBundleVariant: