- Home /
Rotate Object
I'm trying to rotate my gameObject using this script, but I can't control the speed of rotation.
function Update() {
var playerPlane = new Plane(Vector3.up, transform.position);
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hitdist = 0.0;
if (playerPlane.Raycast (ray, hitdist)){
var targetPoint = ray.GetPoint(hitdist);
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = targetRotation;
}
}
Answer by JeffreyD · Mar 01, 2014 at 02:19 PM
I'm new to LookRotation, but Quaterion.Lerp may help. See the example to change the speed of the rotation.
file:///Applications/Unity/Unity.app/Contents/Documentation/Documentation/ScriptReference/Quaternion.Lerp.html
Hope it helps.
Answer by robertbu · Mar 01, 2014 at 03:13 PM
This is usually done with either Quaternion.RotateTowards() or Quaternion.Slerp(). The Slerp() version produces an eased movement. So this line:
transform.rotation = targetRotation;
Becomes:
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, Time.deltaTime * speed);
Or:
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * speed);
'speed' is a variable you define, and the value you need will be different between Slerp and RotateTowards.
Your answer
![](https://koobas.hobune.stream/wayback/20220613135059im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Rotate One Object Around Another 1 Answer
Rotating a Ton of Game Objects 2 Answers
Object follow mouse whilst rotating around object. 1 Answer
Rotate script doesn't work! 1 Answer