Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Using physics to turn my Rigidbody character left/right

Discussion in 'Scripting' started by Palimon, Mar 23, 2015.

  1. Palimon

    Palimon

    Joined:
    Apr 18, 2013
    Posts:
    225
    Hey, I'm fairly new to Unity, and I'm trying to figure out how to get my character to mouseturn. This is a bit weird because I need my character model to be a RigidBody so it can be acted upon externally. Right now I have strafing working, and my mouselook updates the attached camera. I was changing the left/right rotation like this:
    Code (CSharp):
    1. rotationX += Input.GetAxis("Mouse X") * sensitivityX;
    2. rotationX = ClampAngle(rotationX, minimumX, maximumX);
    3. Quaternion xQuaternion = Quaternion.AngleAxis(rotationX, Vector3.up);
    4. this.transform.localRotation = originalBodyRotation * xQuaternion;
    It's from a (or maybe various) character controller tutorials and this worked. But, if my character is acted upon from outside via physics, this breaks. I expect because I am setting the rotation rather than rotating via physics like my strafing. I've been playing around with this for a little while and having difficulty with the math. Right now I'm trying to do it like this:
    Code (CSharp):
    1.  
    2. float mouseXAxis = Input.GetAxis("Mouse X") * sensitivityX;
    3.             if (mouseXAxis != 0 && totalLookTorque < MAX_TURN)
    4.             {
    5.                 bodyRB.AddTorque(new Vector3(0,mouseXAxis, 0));
    6.                 totalLookTorque += mouseXAxis;
    7.             }
    8.             else
    9.             {
    10.                 bodyRB.AddTorque(new Vector3(0,-totalLookTorque,0));
    11.                 totalLookTorque = 0;
    12.             }
    13.  
    It almost works, but there's some bleedthrough where once I start, it always is drifting in one direction or the other, although I can turn properly. Any ideas to clean up my left/right movement? Better over-arching ways to do it?
     
  2. Palimon

    Palimon

    Joined:
    Apr 18, 2013
    Posts:
    225
    Oh, and I know about float imprecision, and the bleedthrough is far more than could be caused by just that. Although I might need to take care of that too, eventually :).
     
  3. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,077
    Isn't there a rotational drag variable on the rigid bodies in the inspector? Try increasing that.

    If not, you could do it yourself by applying another torque proportional to the vertical component of the angular velocity. It's called "damping."
     
    Palimon likes this.
  4. Palimon

    Palimon

    Joined:
    Apr 18, 2013
    Posts:
    225
    There is rotational drag, but I can't use it because that'll negate outside influences too (like if the character is in a spaceship, the spaceship's movement will also be bled away from the player). I ended up doing this which works pretty well for rotating the player with mouse movement:
    Code (CSharp):
    1. // Handle left/right mouselook
    2.             float mouseXAxis = Input.GetAxis("Mouse X") * sensitivityX;
    3.             if (mouseXAxis != 0)
    4.             {
    5.                 bodyRB.AddRelativeTorque(new Vector3(0,mouseXAxis, 0), ForceMode.VelocityChange);
    6.                 hasDampedLR = false;
    7.             }
    8.             else if (!hasDampedLR)
    9.             {
    10.                 //damp spin when there's no longer input from mouse
    11.                 bodyRB.AddRelativeTorque(new Vector3(0, -bodyRB.angularVelocity.y, 0), ForceMode.VelocityChange);
    12.                 hasDampedLR = true;
    13.             }
    Now, almost everything works properly: strafe, look up/down, turn left/right. What doesn't work is if I spin around so I'm facing backwards (or anything in the 180 degrees directly behind my starting position) and I press the forward key, my person flips out like mad. Stays looking in the same direction but it's like he's getting swung around a y-axis and then launched. If you want to trust me, I've hosted the web-player here: http://v01demo.co.nf/
     
    Last edited: Mar 23, 2015
  5. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,077
    Why not subtract the ship's vertical angular velocity component from the player's and use the result for your own damping computation? This way the ship's angular velocity should be ignored since it's been subtracted out.

    If things are going crazy when you rotate to weird angles, it may just be a coordinate system thing like using local instead of world or vice versa somewhere, either in the forces or torques or some vector computation somewhere. When I'm sorting out stuff like that I draw debug lines so I know the directions are all right.
     
  6. Palimon

    Palimon

    Joined:
    Apr 18, 2013
    Posts:
    225
    Oh thanks! I forgot all about visual debugs in Unity. I'll give those a shot and see what's going on. I know that I'm using local coords 100% in the ship and character controller. Unsure if that's correct or not though, I should probably read up Unity coord systems again, see if I'm misunderstanding something.
     
  7. saadali211

    saadali211

    Joined:
    Feb 17, 2017
    Posts:
    19
    Here Is A video Tutorial On Turning Rigidbody Left /Right 90 degree