Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    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.
  5. Kotomine_Kirei


    Jul 27, 2016
    Im having this same issue, and capsule collider isn’t good enough for me (i dont want my character sliding off edges, think movement in Source games). In fact, Source games do exactly what you originally wanted: they use an auto generated bounding box around entites which is used for collision detection, and it is aligned to the world axis so it never rotates, and its exactly what im trying to achieve.
  6. QuinnWinters


    Dec 31, 2013
    If a box collider has corners that hit and cause you issues, and a capsule collider has a rounded bottom that causes sliding on edges or steep inclines, I suggest one of the following two options. 1, make a custom mesh that's a cylinder with a flat bottom so it functions like a box collider but without the corners and use a mesh collider on it, or 2, use raycasting to determine if you're on an edge or slope and use code to prevent the collider from sliding off if it detects that the slope is above a certain angle or the edge is less than a certain distance away from the center of the collider.