简体   繁体   中英

Unity Rigidbody move in one directions

I want the play to holding mouse button 1 and hold, so they go in one direction, but the player can still rotate the player when they are holding click.

Link to my pastebin

MovementController.cs Controls the character movements
Straight.cs On click makes the character, go in a straight line

How would I make it so the player can not rotate while they are holding mouse button 0? I have tried freezing rotation, and rb.Sleep(). Neither of those worked

Rigidbody.freezeRotation , RigidBody.Sleep() and Rigidbody.constraints only forbid the Rigidbody from being rotated when the rotation happens a result of a force being applied.

So it will only stop a rotation from something like Rigidbody.AddTorque or from a collision.

Setting Rigidbody.rotation or calling Rigidbody.MoveRotation will always work regardless of freeze state, constraints or sleep because they mutate the rotation directly; they do not apply a force.

If you want to use Rigidbody.MoveRotation or Rigidbody.rotation or any Transform method, freezing the rotation of the rigidbody or adding constraints won't help. So instead, in MovementControls get mousedown the way you do in Straight and set the rotation only if it's false, like this:

if (!mousedown)
{
    currentRotation.x += Input.GetAxis("Mouse X") * sensitivity;
    currentRotation.y -= Input.GetAxis("Mouse Y") * sensitivity;
    currentRotation.x = Mathf.Repeat(currentRotation.x, 360);
    currentRotation.y = Mathf.Clamp(currentRotation.y, -maxYAngle, maxYAngle);
    rb.rotation = Quaternion.Euler(currentRotation.y, currentRotation.x, 0);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM