2026-01-22 13:18:27 +00:00
|
|
|
using UnityEngine;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
public class GroundEnemyMovement : Entity
|
|
|
|
|
{
|
2026-01-23 10:22:52 +00:00
|
|
|
[SerializeField] private Rigidbody2D rigidBody;
|
|
|
|
|
[SerializeField] private SpriteRenderer spriteRenderer;
|
|
|
|
|
[SerializeField] private float Speed = 3f;
|
|
|
|
|
[SerializeField] private float startDirection = 1.5f;
|
2026-01-22 13:18:27 +00:00
|
|
|
|
|
|
|
|
private float currentDirection;
|
|
|
|
|
private float halfWidth;
|
|
|
|
|
private Vector2 movemenet;
|
|
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
|
{
|
|
|
|
|
halfWidth = spriteRenderer.bounds.extents.x;
|
|
|
|
|
currentDirection = startDirection > 0 ? 1f : -1f;
|
|
|
|
|
spriteRenderer.flipX = currentDirection == 1 ? false : true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-23 10:22:52 +00:00
|
|
|
public override void RunAI()
|
2026-01-22 13:18:27 +00:00
|
|
|
{
|
|
|
|
|
movemenet.x = Speed * currentDirection;
|
|
|
|
|
movemenet.y = rigidBody.linearVelocity.y;
|
|
|
|
|
rigidBody.linearVelocity = movemenet;
|
|
|
|
|
|
|
|
|
|
SetDirection();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetDirection()
|
|
|
|
|
{
|
|
|
|
|
Vector2 frontVec = transform.position;
|
|
|
|
|
Vector2 wallDir = Vector2.right;
|
|
|
|
|
|
|
|
|
|
if (currentDirection > 0)
|
|
|
|
|
{
|
|
|
|
|
frontVec += Vector2.right * halfWidth;
|
|
|
|
|
wallDir = Vector2.right;
|
2026-01-23 10:22:52 +00:00
|
|
|
}
|
|
|
|
|
else
|
2026-01-22 13:18:27 +00:00
|
|
|
{
|
|
|
|
|
frontVec += Vector2.left * halfWidth;
|
|
|
|
|
wallDir = Vector2.left;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RaycastHit2D cliffHit = Physics2D.Raycast(frontVec, Vector2.down, 1f, LayerMask.GetMask("Ground"));
|
|
|
|
|
Debug.DrawRay(frontVec, Vector2.down * 1f, Color.green);
|
|
|
|
|
|
|
|
|
|
float wallDist = halfWidth + 0.2f;
|
|
|
|
|
RaycastHit2D wallHit = Physics2D.Raycast(transform.position, wallDir, wallDist, LayerMask.GetMask("Ground"));
|
|
|
|
|
Debug.DrawRay(transform.position, wallDir * wallDist, Color.blue);
|
|
|
|
|
|
|
|
|
|
if (cliffHit.collider == null || wallHit.collider != null)
|
|
|
|
|
{
|
2026-01-23 10:22:52 +00:00
|
|
|
currentDirection *= -1;
|
|
|
|
|
spriteRenderer.flipX = !spriteRenderer.flipX;
|
2026-01-22 13:18:27 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-01-27 10:20:36 +00:00
|
|
|
|
2026-01-22 13:18:27 +00:00
|
|
|
}
|