Turn to Target slowly
Hi,
I have written an artillery script and for testing I used LookAt() in order to shoot. This is ofc unrealstic when using a cannon from the 19h century, as it immediatley 360 noscopes the enemy. So, I was wondering how I can force the gun to rotate slowly by the given rotate speed (public float). It should rotate towars the enemy (Vector3 enemy_pos)
Do I have to use a coroutine here?
It would be awesome if someone could give me some ideas how I can realize this
Answer by capnjake · Jan 26, 2017 at 02:30 PM
Use this method:
var q = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.RotateTowards(transform.rotation, q, rotateSpeed * Time.deltaTime);
Ignore the below - but keeping it in case it helps someone
Use Transform.Rotate
instead.
https://docs.unity3d.com/ScriptReference/Transform.Rotate.html
float rotateSpeed = 5f;
Vector3 direction = Vector3.right * rotateSpeed;
this.transform.Rotate(direction * Time.deltaTime);
You can change the speed of the rotation by using any direction multiplied by whatever speed you'd like it to turn at.
Hm, I used this before and it did not rotate Vector3 direction = targetPos rotateSpeed; this.transform.Rotate(direction Time.deltaTime); Is my code, targetPos is this: target.transform.position
I believe I misunderstood the question. See the updated post.
Hm, that's something I also tried, doesn't work either