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…
var yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, Vector3.up);
characterBody.transform.localRotation = yRotation * targetCharacterOrientation;
“characterBody” is a reference to the GameObject.
“targetCharacterOrientation” is declared in the following line…
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.
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
private BoxCollider _bc;
void Start()
{
// if only 1 box collider in children
//_bc = GetComponent<Rigidbody>().GetComponentInChildren<BoxCollider>();
// if multiple box colliders in childen then give it a tag
foreach (BoxCollider boxCollider in gameObject.GetComponentsInChildren<BoxCollider>())
{
if (boxCollider.tag == "ManualRotation")
_bc = boxCollider;
}
}
void FixedUpdate()
{
// this example will rotate the collider only on the y axis
_bc.transform.rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0);
}
3 Likes
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.
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.
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.
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.