5

I'm using Unity3D. I'd like to rotate an object to face the direction of the mouse pointer, but allow a maximum rotation speed, like "max 100 degrees per second".

There is an example in the doc, but it does not do what I want.
I think the Time.time should be Time.deltaTime, and I can't really understand what the last parameter does. Is it supposed to be the number that gets summed to the start vector?
http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.Slerp.html

Also, I can't really understand what the last parameter does. Is it a time for the rotation?

The code I'm using now

Plane plane = new Plane(Vector3.up, 0);
float dist;
void Update () {
    //cast ray from camera to plane (plane is at ground level, but infinite in space)
    Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
    if (plane.Raycast(ray, out dist)) {
        Vector3 point = ray.GetPoint(dist);

        //find the vector pointing from our position to the target
        Vector3 direction = (point - transform.position).normalized;

        //create the rotation we need to be in to look at the target
        Quaternion lookRotation = Quaternion.LookRotation(direction);

        //rotate towards a direction, but not immediately (rotate a little every frame)
        transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * rotationSpeed);
    }
}

I think the weak spot is in the third parameter of the Slerp, but I can't figure out what to put there.

2
  • The parameters are: the starting rotation, the ending rotation, and the fraction of the rotation that has been completed so far. When using Time.deltaTime and using the current transform instead of the transform when the rotation first started, then the third parameter is basically how much of the rotation should be done this frame. Is the code you have now working?
    – jonhopkins
    Dec 2, 2013 at 13:40
  • Nope, it was doing an almost immediate rotation.
    – Agostino
    Dec 2, 2013 at 19:00

2 Answers 2

5

This code works, but I'm not sure it's 100% correct.

Plane plane = new Plane(Vector3.up, 0);
float dist;
void Update () {
    //cast ray from camera to plane (plane is at ground level, but infinite in space)
    Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
    if (plane.Raycast(ray, out dist)) {
        Vector3 point = ray.GetPoint(dist);

        //find the vector pointing from our position to the target
        Vector3 direction = (point - transform.position).normalized;

        //create the rotation we need to be in to look at the target
        Quaternion lookRotation = Quaternion.LookRotation(direction);

        float angle = Quaternion.Angle(transform.rotation, lookRotation);
        float timeToComplete = angle / rotationSpeed;
        float donePercentage = Mathf.Min(1F, Time.deltaTime / timeToComplete);

        //rotate towards a direction, but not immediately (rotate a little every frame)
        //The 3rd parameter is a number between 0 and 1, where 0 is the start rotation and 1 is the end rotation
        transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, donePercentage);
    }
}
1
  • I know you should usually avoid comments that just say "Thanks" or whatever, but I have spent days looking for a solution, and although I found many answers... none of them worked right. this worked perfectly, so thank you!
    – Asher
    May 8, 2020 at 20:29
3

You need to maintain a separate variable for the interpolation and update that every frame. Otherwise your Time.deltaTime * rotationSpeed will keep going up forever past the 0-1 range.

private float _RawLerp;
private float _Lerp;
public float _Speed;
public transform _Source;
public transform _Target;

private transform _TransformCache; // the transform for my game object, set in the Awake method

public void Update()
{
    _RawLerp += Time.deltaTime * _Speed;
     _Lerp = Mathf.Min(_RawLerp, 1); 
   _TransformCache.rotation = Quaternion.Slerp(
         _Source.TargetRotation(),
         _Target.TargetRotation(), 
         _Lerp);
}
3
  • Thanks, the point was understanding that the 3rd parameter must be in the 0-1 range. I'll post the fixed code.
    – Agostino
    Dec 2, 2013 at 18:59
  • @user1600770 If theodox answered your question you should accept this answer.
    – Jerdak
    Dec 2, 2013 at 20:54
  • @Jerdak He gave me the right tip, but it's not exactly what I need. I can't keep a fixed target rotation because I need to change it every frame to follow the muouse. I'll give a +1, but I accept the fixed code below. Theodox, is this fair enough for you?
    – Agostino
    Dec 3, 2013 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.