Sprite AI rapidly flipping to try and face the player. This is driving me insane.
So I've got a script that creates a basic FOV mesh, attaches it to the enemy, then sets the aim direction. If the player enters the FOV, I want the aim direction to target the player - but it keeps rapidly flipping back and forth instead.
FOV mesh based off Code Monkey's tutorial: https://www.youtube.com/watch?v=CSeUMTaNFYk&t=786s
I'm sure there are better ways to accomplish my goals in code, but I'm relatively new to C#. Hoping someone can answer this shout into the wind.
public class EnemyController : MonoBehaviour
{
[SerializeField] private EnemyFOV enemyFOV;
[SerializeField] private PlayerChange playerChange;
[SerializeField] private PlayerMovement playerMovement;
public float speed;
public float runSpeed;
private float distanceToStop = 0.1f;
private float waitTime;
public float startWaitTime = 3f;
public Transform[] waypoints;
public Rigidbody2D rb2D;
int waypointIndex = 0;
private float enemyPos;
private Vector3 patrolTarget;
private Vector3 playerPosition;
private Vector3 aimDirection;
private bool isFound;
void Start()
{
isFound = false;
waitTime = startWaitTime;
// Spawn at first waypoint
transform.position = waypoints[waypointIndex].transform.position;
}
void Update()
{
rb2D = GetComponent<Rigidbody2D>();
// Get local scale and velocity
enemyPos = transform.localScale.x;
var localVelocity = transform.InverseTransformDirection(rb2D.velocity);
// Get player position
playerPosition = GameObject.FindGameObjectWithTag("Player").transform.position;
if (isFound == false)
{
FindTargetPlayer();
EnemyPatrol();
if (localVelocity.x > 0.5)
{
transform.localScale = new Vector3 (1, 1, 1);
aimDirection = new Vector3 (1, 0, 0);
}
else if (localVelocity.x < -0.5)
{
transform.localScale = new Vector3 (-1, 1, 1);
aimDirection = new Vector3 (-1, 0, 0);
}
}
if (isFound == true)
{
aimDirection = playerPosition;
StartCoroutine(FollowPlayer());
}
// Set origin point for FOV mesh
enemyFOV.SetOrigin(gameObject.transform.position);
enemyFOV.SetAimDirection(aimDirection);
}
void EnemyPatrol()
{
// Set target waypoint
patrolTarget = waypoints[waypointIndex].transform.position;
// Set direction and apply force
var direction = Vector3.zero;
if(Vector3.Distance(transform.position, patrolTarget) > distanceToStop)
{
direction = patrolTarget - transform.position;
rb2D.AddRelativeForce(direction.normalized * speed * Time.deltaTime, ForceMode2D.Force);
}
// Go to next waypoint
if(Vector3.Distance(transform.position, patrolTarget) < distanceToStop)
{
if(waitTime <=0)
{
waypointIndex += 1;
waitTime = startWaitTime;
}
else
{
waitTime -= Time.deltaTime;
}
}
if(waypointIndex == waypoints.Length)
{
waypointIndex = 0;
}
}
void FindTargetPlayer()
{
if (Vector3.Distance(transform.position, playerPosition) < 15)
{
Vector3 directionToPlayer = (playerPosition - transform.position).normalized;
if (Vector3.Angle(aimDirection, directionToPlayer) < 90 / 2)
{
RaycastHit2D raycasthit2D = Physics2D.Raycast(transform.position, directionToPlayer, 15);
if (raycasthit2D.collider != null)
{
if (raycasthit2D.collider.gameObject.tag == "Player")
{
if(playerChange.playerHiding == false)
{
isFound = true;
}
}
else
{
isFound = false;
}
}
}
}
}
IEnumerator FollowPlayer()
{
var direction = Vector3.zero;
if(Vector3.Distance(transform.position, playerPosition) > distanceToStop)
{
direction = playerPosition - transform.position;
rb2D.AddRelativeForce(direction.normalized * runSpeed * Time.deltaTime, ForceMode2D.Force);
}
yield return isFound = false;
}
}
Comment