Wondering if you could help with something. Im currently making a top down game and I’m having trouble getting a game object to slowly rotate on one axis to face another game object.
Here is the code i’ve tried. Someone on the answers site suggested it.
If it’s only turning on one axis and the game is all on a single plane you could simplify things by just using the z rotation.
If you can calculate the angle between the turret and the player using trigonometry you should be able to set the z rotation using transform.eularAngle.
I can’t get it to rotate over a time limit. I tried Time.deltaTime * 2.0f but it won’t rotate. I checked the docs and it shows in the example its using Time.time. The rotation works but it happens instantly?
Here’s the code,
var dx = turret.transform.position.x - target.transform.position.x;
var dy = turret.transform.position.y - target.transform.position.y;
var radians = Mathf.Atan2(dy,dx);
var angle = radians * 180 / Mathf.PI - 90;
var rotateZ = Mathf.LerpAngle(turret.transform.rotation.z, angle, Time.time);
turret.transform.eulerAngles = new Vector3(turret.transform.rotation.x, turret.transform.rotation.y, rotateZ);
I can’t seem to get it to rotate over time using the LerpAngle?
Scrap that, i worked out what was wrong. I thought I should be multiplying by 2.0 as in seconds but its treated more like speed so it was rotating very slow.