So, I am working on tower defense and now I am doing turrets rotations. I’ve been following tutorial on yt how to do rotation , and they’ve used
Quaternion.Lerp( startpos , enemy , Time.deltaTime*rotationspeed );
in it so I tried this as well.
Problem is , in video towers move smoothly between new targets but my towers just jump around really quickly and it bothers me. I don’t really know what am I doing wrong,
code for rotation:
[SerializeField] private Transform target;
[SerializeField] private float range;
[SerializeField] private Transform parttorotate;
[SerializeField] private float rotationspeed;
void Start()
{
InvokeRepeating("updatetarget", 0f, 0.5f);
}
void updatetarget()
{
GameObject[] enemies = GameObject.FindGameObjectsWithTag("enemy");
float shortestdistance = Mathf.Infinity;
GameObject nearestenemy = null;
foreach (GameObject enemy in enemies)
{
float distanceToenemy = Vector3.Distance(transform.position, enemy.transform.position);
if (distanceToenemy < shortestdistance)
{
shortestdistance = distanceToenemy;
nearestenemy = enemy;
}
}
if(nearestenemy != null && shortestdistance<=range)
{
target = nearestenemy.transform;
}
else
{
target = null;
}
}
void Update()
{
if (target == null)
{
return;
}
else
{
Vector3 dir = target.position-transform.position;
Quaternion lookrotation = Quaternion.LookRotation(dir);
//rotation
Vector3 rotation = Quaternion.Lerp(parttorotate.rotation, lookrotation,
rotationspeed*Time.deltaTime).eulerAngles;
parttorotate.rotation = Quaternion.Euler(-90f,0f, rotation.y);
//-90 on X is because of my model, I think I designed it wrong in blender and now axis are not okay XD
}
}
and video I’ve been following: