Fix enemy stucking
This commit is contained in:
parent
eaed21b897
commit
143d9dc200
2 changed files with 56 additions and 6 deletions
|
@ -11,7 +11,7 @@ GameObject:
|
|||
- component: {fileID: 7051718864318870241}
|
||||
- component: {fileID: 5205340436538485392}
|
||||
- component: {fileID: 9038174535399619494}
|
||||
m_Layer: 0
|
||||
m_Layer: 9
|
||||
m_Name: Sprite
|
||||
m_TagString: Untagged
|
||||
m_Icon: {fileID: 0}
|
||||
|
@ -111,7 +111,7 @@ GameObject:
|
|||
- component: {fileID: 8277411166637495994}
|
||||
- component: {fileID: 1412018915745597861}
|
||||
- component: {fileID: 662054772667612550}
|
||||
m_Layer: 0
|
||||
m_Layer: 9
|
||||
m_Name: Bomb
|
||||
m_TagString: Bomb
|
||||
m_Icon: {fileID: 0}
|
||||
|
@ -198,7 +198,7 @@ GameObject:
|
|||
m_Component:
|
||||
- component: {fileID: 6508079771898401246}
|
||||
- component: {fileID: 2175931883523803554}
|
||||
m_Layer: 0
|
||||
m_Layer: 9
|
||||
m_Name: 2DCollider
|
||||
m_TagString: Bomb
|
||||
m_Icon: {fileID: 0}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
using UnityEngine;
|
||||
using src.Helpers;
|
||||
using src.Helpers;
|
||||
using src.Managers;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public abstract class EnemyBase : MonoBehaviour, IExplosable
|
||||
{
|
||||
|
@ -12,6 +14,10 @@ public abstract class EnemyBase : MonoBehaviour, IExplosable
|
|||
protected float Speed { get; set; }
|
||||
protected Vector2 Direction { get; set; }
|
||||
|
||||
private List<Vector3> _allowedDirections = new List<Vector3>();
|
||||
private bool _isStucked = false;
|
||||
private System.Random _random = new System.Random();
|
||||
|
||||
// Start is called before the first frame update
|
||||
protected void Start()
|
||||
{
|
||||
|
@ -23,6 +29,10 @@ public abstract class EnemyBase : MonoBehaviour, IExplosable
|
|||
protected void FixedUpdate()
|
||||
{
|
||||
if (gameStateManager.IsGamePaused || gameStateManager.IsPlayerMovementForbidden) {return;}
|
||||
if(_isStucked)
|
||||
{
|
||||
Unstuck();
|
||||
}
|
||||
Rigidbody2d.MovePosition(Rigidbody2d.position + Direction * Speed * Time.deltaTime);
|
||||
}
|
||||
|
||||
|
@ -42,7 +52,13 @@ public abstract class EnemyBase : MonoBehaviour, IExplosable
|
|||
public void OnCollisionEnter2D(Collision2D col)
|
||||
{
|
||||
MoveToCenterOfTheCell();
|
||||
Direction = _directions.ChoseRandomExcept(Direction);
|
||||
Unstuck();
|
||||
}
|
||||
|
||||
public void OnCollisionStay2D(Collision2D col)
|
||||
{
|
||||
MoveToCenterOfTheCell();
|
||||
Unstuck();
|
||||
}
|
||||
|
||||
protected void MoveToCenterOfTheCell()
|
||||
|
@ -62,4 +78,38 @@ public abstract class EnemyBase : MonoBehaviour, IExplosable
|
|||
{
|
||||
return _directions.ChoseRandomExcept(direction);
|
||||
}
|
||||
|
||||
private void Unstuck()
|
||||
{
|
||||
_allowedDirections.Clear();
|
||||
StartCoroutine(CheckForObstacle(Vector3.down));
|
||||
StartCoroutine(CheckForObstacle(Vector3.left));
|
||||
StartCoroutine(CheckForObstacle(Vector3.up));
|
||||
StartCoroutine(CheckForObstacle(Vector3.right));
|
||||
if(_allowedDirections.Count == 0)
|
||||
{
|
||||
_isStucked = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
var index = _random.Next(_allowedDirections.Count);
|
||||
Direction = _allowedDirections[index];
|
||||
_isStucked = false;
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator CheckForObstacle(Vector3 direction)
|
||||
{
|
||||
var layerMask = (1 << 8) | (1 << 9);
|
||||
var currentPosition = transform.position;
|
||||
|
||||
var hit = Physics2D.Raycast(new Vector2(currentPosition.x + 0.5f, currentPosition.y + 0.5f), direction, 1, layerMask);
|
||||
|
||||
if (!hit.collider)
|
||||
{
|
||||
_allowedDirections.Add(direction);
|
||||
}
|
||||
|
||||
yield return new WaitForSeconds(0.05f);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue