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;

I’m new to LookRotation, but Quaterion.Lerp may help. See the example to change the speed of the rotation.


Hope it helps.

This is usually done with either Quaternion.RotateTowards() or Quaternion.Slerp(). The Slerp() version produces an eased movement. So this line:

  transform.rotation = targetRotation;


  transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, Time.deltaTime * speed);


  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.