Unity3D Slerp rotation speed

Agostino

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.

Agostino

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);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unity3D Slerp rotation speed

From Dev

Transform rotation (Slerp) - Unity

From Dev

Unity Slerp Rotation Moved back to original rotation - not working correctly with negative numbers

From Dev

Rotation angle constraints in Unity3d?

From Dev

Unable to change rotation in Unity3D

From Dev

Unity object rotation speed differs on device

From Dev

Unity Lerp and Slerp not moving smoothly

From Dev

Tween camera position while rotation with slerp -- THREE.js

From Dev

Get accurate Rotation Angle in Unity3D C#

From Dev

how to limit the rotation of a game object in unity3d

From Dev

Theta, Phi to Quaternion rotation Unity3D C#

From Dev

Unity3D Character Controller set speed through script?

From Dev

Unity3D why multiply axis with SPEED?

From Dev

How to change rigs spine rotation in unity3d, using camera rotation?

From Dev

OpenCV rotation (Rodrigues) and translation vectors for positioning 3D object in Unity3D

From Dev

2D top-down point and click movement and rotation in Unity3D

From Dev

How do I limit head rotation in Unity3D Google Cardboard?

From Dev

How can I find the rotation of the head in Google Cardboard Unity3D?

From Dev

Camera Rotation Issue in Unity3D C# (likely easy fix)

From Dev

How can I find the rotation of the head in Google Cardboard Unity3D?

From Dev

How can i raise the wall in unity3d once and control the raising speed?

From Dev

In Unity3d, how to work out speed required to travel distance at specific time, cannonball with Sin()

From Dev

Rotation in unity

From Dev

Rotate sprite by touch with a limited rotation speed

From Dev

How to speed up rotation operations in Python?

From Dev

Is there a way to control my hard-disk rotation speed?

From Dev

How to decrease rotation speed in qml depending from the time?

From Dev

Unity BoxCollider2D rotation not the same as GameObject rotation

From Dev

unity rotation difference and direction

Related Related

  1. 1

    Unity3D Slerp rotation speed

  2. 2

    Transform rotation (Slerp) - Unity

  3. 3

    Unity Slerp Rotation Moved back to original rotation - not working correctly with negative numbers

  4. 4

    Rotation angle constraints in Unity3d?

  5. 5

    Unable to change rotation in Unity3D

  6. 6

    Unity object rotation speed differs on device

  7. 7

    Unity Lerp and Slerp not moving smoothly

  8. 8

    Tween camera position while rotation with slerp -- THREE.js

  9. 9

    Get accurate Rotation Angle in Unity3D C#

  10. 10

    how to limit the rotation of a game object in unity3d

  11. 11

    Theta, Phi to Quaternion rotation Unity3D C#

  12. 12

    Unity3D Character Controller set speed through script?

  13. 13

    Unity3D why multiply axis with SPEED?

  14. 14

    How to change rigs spine rotation in unity3d, using camera rotation?

  15. 15

    OpenCV rotation (Rodrigues) and translation vectors for positioning 3D object in Unity3D

  16. 16

    2D top-down point and click movement and rotation in Unity3D

  17. 17

    How do I limit head rotation in Unity3D Google Cardboard?

  18. 18

    How can I find the rotation of the head in Google Cardboard Unity3D?

  19. 19

    Camera Rotation Issue in Unity3D C# (likely easy fix)

  20. 20

    How can I find the rotation of the head in Google Cardboard Unity3D?

  21. 21

    How can i raise the wall in unity3d once and control the raising speed?

  22. 22

    In Unity3d, how to work out speed required to travel distance at specific time, cannonball with Sin()

  23. 23

    Rotation in unity

  24. 24

    Rotate sprite by touch with a limited rotation speed

  25. 25

    How to speed up rotation operations in Python?

  26. 26

    Is there a way to control my hard-disk rotation speed?

  27. 27

    How to decrease rotation speed in qml depending from the time?

  28. 28

    Unity BoxCollider2D rotation not the same as GameObject rotation

  29. 29

    unity rotation difference and direction

HotTag

Archive