I have a top down Game, some turrets and enemies. The turrets rotate to the enemy and the enemy follows a path and rotate (follows) the generated path line.
I need to know when the turret completed the rotate so I can implement relevant code here
My scripts are:
Turrets chasing the enemies
private void OnTriggerStay2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Enemy"))
{
Vector3 enemyPosition = collision.transform.position;
Vector3 vectorToTarget = (enemyPosition - transform.position).normalized;
float Angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
Quaternion q = Quaternion.AngleAxis(Angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * RotationSpeed);
inVision = true;
}
}
Enemies moving and rotating correctly to the generated path line:
Vector2 direction = ((Vector2)path.vectorPath[currentWaypoint] - rb.position).normalized;
SteerTowardsTarget(direction);
Vector2 force = direction * Speed * Time.deltaTime;
rb.AddForce(force);
void SteerTowardsTarget(Vector2 direction)
{
float targetRot = Vector2.Angle(Vector2.right, direction);
if (direction.y < 0.0f)
targetRot *= -1;
float rot = Mathf.MoveTowardsAngle(transform.localEulerAngles.z, targetRot, 1.0f);
transform.eulerAngles = new Vector3(0f, 0f, rot);
}