Search Unity

  1. We want you to join us at GDC this year! Take a peek at all of the events we will be hosting during the week of GDC.
    Dismiss Notice
  2. Tell us about your experience here and you’ll get early access to the 2018 Game Studios report + more goodies.
    Dismiss Notice
  3. Unity 2017.3 has arrived! Read about it here.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  5. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice

Freeze collider rotation?

Discussion in 'Scripting' started by Denisowator, Aug 26, 2017.

  1. Denisowator


    Apr 22, 2014
    I have the following setup for controlling my character:
    A player GameObject, with a box collider, Rigidbody and a movement script. (script uses transform.Translate to move character)
    A child camera, which is the main camera, with a control script. Part of the script rotates the whole GameObject with itself (only on the Y axis), using the following lines...
    Code (CSharp):
    1. var yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, Vector3.up);
    2. characterBody.transform.localRotation = yRotation * targetCharacterOrientation;
    "characterBody" is a reference to the GameObject.
    "targetCharacterOrientation" is declared in the following line...
    Code (CSharp):
    1. var targetCharacterOrientation = Quaternion.Euler(targetCharacterDirection);
    The problem I'm having, is that because the collider rotates, there is a lot of jittery motion when pushing against not perfectly parallel surfaces. And generally I just don't want the physics to be affected by the player turning.

    The only question I found on this (and also the only solution), was this. It's pretty outdated, not to mention the solution obviously doesn't work for me.

    What ends up happening, is local movement gets messed up, and the player just moves in world space, no matter where I'm turning.

    So I'm back at square one. All I need to do is prevent the collider from rotating, but I don't know how.

    P.S. Just in case it wasn't clear, this is for a first person game.
    Last edited: Aug 26, 2017
  2. unit_nick


    Jul 15, 2017
    You could create an empty game object as a child of your object.
    Attach a box collider to the empty game object
    Optionally add a tag to the box collider if you want to find it
    Move the box collider as you see fit

    Code (csharp):
    2. private BoxCollider _bc;
    4. void Start()
    5. {      
    6.     // if only 1 box collider in children
    7.     //_bc = GetComponent<Rigidbody>().GetComponentInChildren<BoxCollider>();
    9.     // if multiple box colliders in childen then give it a tag
    10.     foreach (BoxCollider boxCollider in gameObject.GetComponentsInChildren<BoxCollider>())
    11.     {
    12.         if (boxCollider.tag == "ManualRotation")
    13.             _bc = boxCollider;
    14.     }
    15. }
    17. {
    18.     // this example will rotate the collider only on the y axis
    19.     _bc.transform.rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0);
    20. }
    sdutter and Denisowator like this.
  3. Denisowator


    Apr 22, 2014
    So, I just revisited this issue, and I notice a problem with this solution. First, the collider jitters when I turn. So much so that it when I'm up against a wall, and I make my character turn a bunch of times, I might as well have had a fully moving collider. By the way I made Quaternion.Euler (0, 0, 0) because I don't want the collider to turn at all).

    Secondly, even if I put it in Update instead of FixedUpdate, it still jitters. And enough for it to still affect the player when turning.

    What's basically happening is the collider rotation updates before it gets set to "0, 0, 0". Causing very slight "jittering" when I turn. So if I'm next to a collider, and I turn, I still get pushed away since the corner of the my collider hits the other one while turning.

    Just fyi, this isn't really necroposting imo. I just don't want to be making a new thread when there's already one made by me, with the exact same name.

    Right now I'm changing the Rigidbody's velocity rather than changing the object's transform position.
  4. Denisowator


    Apr 22, 2014
    Nevermind, I decided to use a capsule collider instead. The reason I wanted to completely freeze the collider is because it was a BoxCollider, so if I turned, the corners would hit the walls and push the player away.