- Home /
OnTrigger functions only work when in motion?
I'm trying to detect when an object has entered a trigger parented to my Player object. Everything worked fine with this code:
function OnTriggerStay ()
{
collided = true;
}
function OnTriggerExit ()
{
collided = false;
}
But now when I'm setting my Character's position to a specific point in 3D space, OnTriggerStay ends up being called even when the collider is obviously not touching anything:
So I did some research and everyone suggests that the rigidbody has fallen asleep. So, I called the WakeUp function every frame to ensure that it doesn't sleep, and it still detects a non-existent collision. Keep in mind that when we start moving again the detection appears to work fine, it returns back to no collision.
After setting the Collision Detection to Continuous and Continuous Dynamic and still no change in behavior. So I figured that maybe the OnTriggerStay function is broken, so I wrote my own version to replicate the effect I want:
var enters : Array;
function Awake () {
enters = new Array ();
}
function OnTriggerEnter(other : Collider)
{
enters.Push(other.gameObject);
}
function OnTriggerExit(other : Collider)
{
for (var c = 0; c < enters.length; c++)
{
if (other.gameObject == enters[c])
enters.RemoveAt(c);
}
}
and then I check to see if the length of the "enters" array is 0, and if it is then we know an object is not inside it.
This code was able to replicated the OnTriggerStay effect, the collided variable still turns on when we're not in motion. This leads me to believe that all of the OnTrigger functions are bugged.
How can I prevent these functions from calling a non-existent collision?
Other Things I have tried:
Calling
CharacterController.SimpleMove(Vector3.zero)
transform.position += Vector3.zero
motor.inputMoveDirection = Vector3.zero
I heard in my research somewhere that doing these can "cheat" motion for the CharacterController, but none of these have worked.
I tried adding a rigidbody to the moving object--didn't work. Neither did adding a rigidbody to the objects that enter the trigger. I did some Debugs and found out that the random collisions were from objects that were close by, but still not touching the colliders.
I even did some frame result tests and found that the Collision function was indeed happening before the Update and FixedUpdate functions that set the value to true, which is how it should work.
I really want to crush this bug, any help would be appreciated!
Answer by SomeGuy22 · Mar 08, 2015 at 02:20 AM
After working all day on this with no explanation or documentation or help I deduced that the problem lay not in the Collision Functions, but with the CharacterController/Motor functions. What was happening was I was manually setting the position of my character, but the velocity was still being calculated.
It's as if his downwards momentum was building up as he was hanging in the air. Therefore, the physics system tried to compensate by detecting things in advance, as if he was falling at the speeds being built up. For those of you in the future reading this with this problem, you'll need to reset motor velocity:
function ResetVel()
{
motor.SetVelocity(Vector3.zero);
}
For every frame where the character's position is being externally moved.