Triggers move my character controller

Hi, this is my first time posting and i dont believe this to be an issue with scripting or anything like that so I’ve posted in General Support. Please move if necessary.

Basically, i have a few sphere colliders in my scene, using them as triggers.

However, whenever my First Person Controller moves through these triggers he seems to get nudged a little bit. As in, he’s colliding with them for a brief moment before passing through them.

Can anyone advise if this is a known unity bug or is there a setting I can modify?

Cheers.

Are you using the Standard Assets FPS Controller? I had the same issue a while back, and it turned out it was because the Standard Assets FPS Controller does some raycasting to keep itself grounded on slopes, but the raycast is set to go against all layers, so it hits triggers too. Part of the ‘stay grounded’ code consists of shifting the player down onto the ground, which it finds using that raycast. When doing this it finds the sphere triggers and then shifts the player down a bit, even though those spheres aren’t part of the ground.

To fix it, I ended up adding some code to tell it which layers to do that raycast against, and put the triggers on a different layer. You could also put them in the IgnoreRaycast layer, but then your code won’t be able to do raycasts on them either. Depending on what they’re for, that might work out for you.

All that said, this issue should be fixed in Unity 5.2+. Unity added a QueryTriggerInteraction parameter to all raycast/spherecast/etc functions, that allows you to choose whether that particular cast will hit triggers (and in their FPS Controller they have it ignore them).

Thanks for the answer mate! It was really starting to bug me as i thought i was doing something wrong!

I have added them to the Ignore Raycast layer and it is sorted, although I suspect other problems will arise later if I need to use some raycasting…

Do you by any chance have the code you created to hand?

Cheers!

Unfortunately I can’t upload the entire file, but I can show you the edits I made. It was a pretty simple change overall.

In FirstPersonController.cs (located at [ProjectName]\Assets\Standard Assets\Characters\FirstPersonCharacter\Scripts\FirstPersonController.cs), I added:

[SerializeField]
private LayerMask m_groundLayers;

to the top of the file where all the other variable declarations are.

Then at the line where the Physics.SphereCast is done, I changed it to:

Physics.SphereCast(transform.position, m_CharacterController.radius, Vector3.down, out hitInfo, m_CharacterController.height / 2f, m_groundLayers.value);

The only change there being that I specified the m_groundLayers variable as the layer to raycast against. m_groundLayers will show up in the inspector, and you can pick which layers to use as the ground, and then just make sure your triggers are not on any of those layers.

This edit, by the way, is for the Unity 5 version of the First Person Controller. As far as I remember the Unity 4 one does not have this problem.