- Home /
The question is answered, right answer was accepted
How to check for collision between a moving kinematic object and an empty object?
Hello, Unity Community!
I have an elevator (sprite) in my platformer, but I am having trouble making it stop.
I am moving it using this: rigidbody2D.MovePosition(rigidbody2D.position + Vector2.up* Time.deltaTime);
, while a variable moving
is true;
I have also created an empty game object and attached a collider to it. Now, I have this code here that should detect when my elevator collides with the empty game object, and then make the variable moving
false.
void OnCollisionEnter2D(Collision2D coll) {
if (coll.gameObject.tag == "ElevatorStop")
{
moving=false;
}
}
Now, to clear some questions that might arise: the empty game object has the "ElevatorStop" tag, the layers on which both objects are found can interact, and both objects have "isKinematic=true".
What am I doing wrong? Or, how would this be achieved with an alternate solution? The solution should be general, because I plan on attaching this script to a prefab.
EDIT: After a bit of tweaking, I noticed that " if (coll.gameObject.tag == "ElevatorStop")
" is never true. Ideas?
Answer by Kiwasi · Aug 28, 2014 at 06:34 PM
Two kinematic colliders will never collide.
Setting isKinematic tells the engine "Don't worry about adding forces to this, I'm going to control it by script".
Two options to solve
Set isKinematic to false on the elevator stop
Change the collider on elevator stop to a trigger and use OnTriggerXXX
Oh, hey, it's you! You've answered my question before. Anyways, thanks. I'm using OnTrigger now, works great.
Answer by AngryBurritoCoder · Aug 28, 2014 at 03:16 PM
Why don't you just animate the elevator to go up? It's easy and simple for an upward or downward motion
Answer by Doodlemeat · Aug 28, 2014 at 05:53 PM
You have to give your empty gameobject a collider. I would give it a boxcollider. You cant detect collision between an empty game object and an object with a collider attached to it.
Sorry, I already specified this in my original question: "I have also created an empty game object and attached a collider to it."
Follow this Question
Related Questions
Ball bounces in the wrong direction 1 Answer
Colliders in a wall jut out 0 Answers
How can I see my physics2d joints? 0 Answers
Using child colliders with rigidbodies/joints in 2D 0 Answers
OnTriggerEnter and OnTriggerExit both trigger twice 0 Answers