- Home /
Response speed in OnCollisionEnter / OnTriggerEnter
I'm attempting to set up a simple 2D prototype that uses OnCollisionEnter (I've also tested OnTriggerEnter) to stop a moving object at precise intervals, but having some problems that seem like they may be related to when / how those functions are called.
Here's what I have / need:
10 platforms spaced out at exact intervals
A "falling" object (which I'm actually moving with a Lerp in Update; the object itself is kinematic / not affected by gravity)
As the object falls, it should hit each platform and stop on it until set to be falling again
The platforms trigger / call OnCollisionEnter properly each time the falling object hits them. However, each time I run the prototype, some of the platforms malfunction, letting the object fall partway through before changing the variable that stops it.
Some previous answers I've seen suggest that OnTriggerEnter uses physics and thus may call too infrequently for use with an object that's moving quickly, but it's suggested that OnCollisionEnter should be "faster". I'm seeing the same problem with both, though. Is there some way I can get either to work, or some obvious problem with using them in this way?
For reference, here's the simple code I'm trying to use.
This is on the falling object:
void Update () {
if (falling) {
originalPos = this.transform.position;
targetPos = this.transform.position + new Vector3 (0f, -0.2f, 0f);
this.transform.position = Vector3.Lerp (targetPos, originalPos, fallRate);
if (fallRate > -1f) {
fallRate = fallRate - 0.01f;
}
}
}
And this is on each platform:
void OnCollisionEnter (Collision piece) {
PieceScript stopIt = piece.gameObject.GetComponent<PieceScript>();
stopIt.falling = false;
Debug.Log ("platform triggered:" + piece.gameObject.name);
}
Yep.. that's correct Bunny. I got so focused on the rate of collision / trigger that I forgot to think about how I was moving the object. Thank you!
If I set the transform lerp to a lower speed, the pieces align perfectly on the platforms, so... yes, more or less. I'll just have to fiddle with how they move, I guess.
sweet, good job, im gonna upgrade my post to an answer :)
Answer by Bunnybomb7670 · Jul 14, 2013 at 11:34 AM
Exactly how fast is the object moving at? if it is moving too fast, it wont pick it up because technically, when an object moves, it is teleporting every frame, the faster you get, the bigger the jump between frames, if it is going so fast, it may teleport straight past the object therefore not triggering it.
Your answer
Follow this Question
Related Questions
simple onCollision and onTrigger problem 1 Answer
OnTriggerEnter do not triggers 1 Answer
On Trigger Enter, Collide with object, specific collision 1 Answer
Colliding two GameObjects 1 Answer