Search Unity

Rigidbody with compound collider rotating on frozen rotation axes

Discussion in 'Physics' started by MDragon, Jun 17, 2015.

  1. MDragon

    MDragon

    Joined:
    Dec 26, 2013
    Posts:
    329
    Hello there~

    I'm currently testing a system that uses both compound colliders with a single rigidbody on a character, moving via setting the velocity of the object. I can move forward and backward, as well as turn rotate sideways (by setting velocity and angular velocity respectively). I initially tested with a single cube and all worked well- my object wasn't turning on the X and Z axes (which are frozen in the rigidbody options).

    However, the issue comes in with compound colliders:



    Normally, the "player" is standing upright. However, once the player moves forward and the "arm" runs into one of the three stationary blocks, the entire player falls forward- even though the rotation is clearly frozen on the X and Z axes.

    As a side note, this also occurs when gravity is enabled on the player and the player tries to move forward. Seems like friction slides the bottom two "legs" back so the player tips forward slightly as well.

    Here is my current prototyping movement script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BasicPlayerMovement : MonoBehaviour {
    5.  
    6.     public float speedMultiplier = 1f;
    7.  
    8.     private Rigidbody thisRigidbody;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         thisRigidbody = this.GetComponent<Rigidbody> ();
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.  
    18.     }
    19.  
    20.     void FixedUpdate() {
    21.         float vertInput = Input.GetAxis ("Vertical");
    22.         float horizInput = Input.GetAxis ("Horizontal");
    23.        
    24.         //transform.position += (transform.forward * vertInput + transform.right * horizInput) * Time.deltaTime;
    25.         Vector3 newVel = transform.forward * vertInput;
    26.         Vector3 newAngularVel = Vector3.up * horizInput;
    27.  
    28.         thisRigidbody.velocity = newVel;
    29.         thisRigidbody.angularVelocity = newAngularVel;
    30.  
    31.     }
    32. }
    The only solution I can see is to save the rotation every frame, keep track of my intended changes, and at the end of every Update() cycle, to reapply the old rotation if necessary. However, is there a way built into the physics system itself to make sure this odd rotation doesn't happen? Keeping track of the rotation manually seems to be an inefficient, sort of hacky solution.

    Thanks!