Question by
Qhailashnikov · Apr 11, 2020 at 02:22 AM ·
rotationaiwaypointwaypointswaypoint system
AI Waypoint rotate problem
Good day, I am current working on a tower defense project and I ran into a bug or glitch when the enemy tries to rotate mainly for enemies with higher speed. The enemy stuck in current position and glitching back and forth at sometime. Here I'l show you Problem
public class EnemyMovement : MonoBehaviour
{
private Transform target;
private int wavePointIndex = 0;
public float turnSpeed = 5f;
public Transform partToRotate;
private Enemy enemy;
private void Start()
{
enemy = GetComponent<Enemy>();
target = Waypoints.points[0];
}
private void Update()
{
Vector3 dir = target.position - transform.position;
Vector3 relativePos = target.position - transform.position;
Quaternion lookRotation = Quaternion.LookRotation(relativePos);
Vector3 rotation = Quaternion.Lerp(partToRotate.rotation, lookRotation, turnSpeed * Time.deltaTime ).eulerAngles;
partToRotate.rotation = Quaternion.Euler(0f, rotation.y , 0f);
transform.Translate(dir.normalized * enemy.speed * Time.deltaTime, Space.World);
if (Vector3.Distance(transform.position, target.position) <= 0.4f)
{
GetNextWayPoint();
}
enemy.speed = enemy.startSpeed;
}
Thanks in forward
Comment