- Home /
How do I control the speed of a rotating rigidbody?
I have an object rotating to always face the mouse, but the rotation is instant. I would like to slow the rotation down so the user slowly turns to face the mouse pointer.
I am using the code from here: https://unity3d.com/learn/tutorials/projects/survival-shooter
Here is the snippet of code I am having problems chaning:
// Create a ray from the mouse cursor on screen in the direction of the camera.
Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
// Create a RaycastHit variable to store information about what was hit by the ray.
RaycastHit floorHit;
// Perform the raycast and if it hits something on the floor layer...
if(Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))
{
// Create a vector from the player to the point on the floor the raycast from the mouse hit.
Vector3 playerToMouse = floorHit.point - transform.position;
// Ensure the vector is entirely along the floor plane.
playerToMouse.y = 0f;
// Create a quaternion (rotation) based on looking down the vector from the player to the mouse.
Quaternion newRotation = Quaternion.LookRotation (playerToMouse);
// Set the player's rotation to this new rotation.
playerRigidbody.MoveRotation (newRotation);
}
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Collider/Rigidbody issue.Camera shaking! 3 Answers
Picked Up object - how to create correct collision detection 0 Answers
How do I add force to rigidbody when raycast hits it. 1 Answer