- Home /
Continuously rotating horizontally a player with the mouse position
Hi!
I'm quite new with Unity and I'm struggling with an issue. So right now I have a player with rigidbody that I can rotate horizontaly using this :
float _yRot = Input.GetAxisRaw("Mouse X");
Vector3 rotation = new Vector3(0, _yRot, 0) * lookSensitivity;
rb.MoveRotation(rb.rotation * Quaternion.Euler(rotation));
But with this, lets say I put my mouse to the left of the screen and don't move it anymore. The variable _yRot become something like -2, my player rotates until _yRot = 0 and now face the new direction.
What i'm trying to achieve is that if I put my mouse on the left and don't move it anymore (and _yRot = -2) : my player rotates to the left indefinitely at a speed proportional to _yRot. And I could make him stop rotating by placing the mouse in a "dead zone" at the center of the screen.
I'm learning and I have some issues understanding rotations, quaternions... If someone see what I'm trying to do and could give me some advice it would be really nice :) Thanks!
Sorry but I don't think I understand the issue. You want your player to rotate to a point indefinitely? How is that possible?
Well that was the question yeah, the code given by Tobychappell do it :) Like you "indicate a rotation direction" to the player.
Answer by Tobychappell · May 25, 2018 at 10:53 PM
Consider trying something like this?
Rigidbody rb;
float lookSensitivity;
float deadZone = 100;
void Update()
{
float _yRot = Input.mousePosition.x - (Screen.width/2.0f);
if (Mathf.Abs(_yRot) > deadZone)
{
Vector3 rotation = new Vector3(0, _yRot, 0) * lookSensitivity;
rb.MoveRotation(rb.rotation * Quaternion.Euler(rotation));
}
}
Thank you so much! I didn't know the Input.movePosition command and was only finding some informations on how rotate the player with mouse but not with this line. It work just fine thanks :)