- Home /
Turret: Getting a constant rotation speed
After doing a tornado twins tutorial, I have code which makes a turrent rotate toward the player. Which is good. But the rotation speed is not constant - the closer it gets to aiming directly at the player, the more it slows down the rotation of the turrent.
I would like it to just use a constant rotation speed until it achieves a perfect aim at the player.
I think the following is the relevant code, but I am not sure. May the unity gods forgive me...
if (LookAtTarget)
{
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
}
Answer by syclamoth · Mar 15, 2012 at 07:02 AM
Yes, that is the relevant code. If you want it to rotate at a constant speed, replace that line with this:
// assuming you have a variable called 'rotationSpeed'
transform.rotation = Quaternion.RotateTowards(transform.rotation, rotate, rotationSpeed * Time.smoothDeltaTime);
Hi $$anonymous$$ieren, thanks for your reply!
Well it does seem to rotate at a constant, but it's doing so incredibly slowly (though visibly so). I defined rotationSpeed, setting it to 20, then 2000, but it seems the same very, very slow turning speed in either case (no change in speed)? Were getting somewhere, just need to be able to adjust the turn speed, please!
Well, keep in $$anonymous$$d that if 'rotate' is close, it will only rotate to that point (no further).
Okay, it's weird I open up unity again but this time the turret instantly faces the player character. No turning time at all. I undid all changes back to what the script was at load and it still does it (that's difficult when it appears unity does one thing on the previous edit then another thing on a second load).
I definately don't want the turrent to instantly face the player either (I want it to turn toward them at an even rate of speed). I'm sorry if I'm not getting this. Before it was too slow, now it's instant?
Well, assu$$anonymous$$g that rotationSpeed is a paramater that you set in the inspector, make sure that it's set to something sane. It occurs to me that if you are assu$$anonymous$$g that changes you make to the initial value in the script file will do anything at all to serialized fields, you could be having problems. Rely on the inspector, not on the code.
And yup, there's the tip I need to learn! Watch the inspector, not the script! Yes, it was set to 1 in the inspector - I adjusted it up and it controls the turning speed! Thank you, syclamoth! It's going nicely now! :)
Answer by Ebil · Mar 16, 2012 at 07:08 AM
Looks like an error which occurs at runtime, then the script isnt used. I also experienced problems i was working on for hours, next day open unity again, and everything is working fine...