Search Unity

Trying to rotate a Rigidbody with Collider

Discussion in 'Physics' started by paulbk, Dec 13, 2017.

  1. paulbk

    paulbk

    Joined:
    Nov 28, 2017
    Posts:
    7
    Hey All,

    I have a 3D rigidbody with a collider that moves forward off of a 3D platform. from there, I'm trying to manipulate the rotation of the person while in air. I've been able to accomplish this, but not the way I want. right now my code will continuously rotate the person until I tell it to change directly. What I want to have happen is to change the rotation of the person a few degrees selectively (for example, when you press A it rotates forward only a few degrees, when you left go of A it stops) until its at the angle I want. any thoughts on how to accomplish this?

    Thanks!

    Code (CSharp):
    1.     void FixedUpdate()
    2.     {
    3.         float moveHorizontal = Input.GetAxis("Horizontal");
    4.         float moveVertical = Input.GetAxis("Vertical");
    5.         Vector3 rotation = new Vector3(moveHorizontal,0.0f,0.0f);
    6.         Vector3 movement = new Vector3(0.0f, 0.0f, moveVertical);
    7.         rb.AddForce(movement * speed);
    8.         rb.AddTorque(transform.right * moveHorizontal * tilt,ForceMode.VelocityChange);
    9.        
    10.         if (onGround)
    11.         {
    12.             if (Input.GetKeyDown(KeyCode.Space))
    13.             {
    14.                 rb.velocity = new Vector3(0f, 5f, 0f);
    15.                 onGround = false;
    16.                 //rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
    17.             }
    18.         }
    19.     }
     
  2. BoogieD

    BoogieD

    Joined:
    Jun 8, 2016
    Posts:
    236
    You don't usually change rigidbody velocity.
     
  3. paulbk

    paulbk

    Joined:
    Nov 28, 2017
    Posts:
    7
    Yea, I was just testing something and forgot to remove it and remove the comments on rb.addforce
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You have to include a GetKey or GetButton (if statements) that wrap around the rotation code so it only does so while the key is held down. :)