- Home /
Question by
Zardium · May 07, 2014 at 03:01 AM ·
collisionrigidbodyrigidbody-collision
How to make an object only respond to a character?
Hi! I have an object in my game (the first level) that is a coin. It has code to teleport the player to the next level when it is touched. However, the game involves dragging rigidbodies around to get to the coin, and i the rigidbody touches the coin it skips to the next level. How can I make it so the coin only responds to the character?
Comment
If you are using OnCollisionEnter(),
void OnCollisionEnter(Collision coll)
{
if(coll.game object == playerObject)
{
//teleport to next level
}
}
This can easily be implemented in 2d also.
Yes! I'm testing it right now and it seems to be working. Thanks!
Answer by thornekey · May 07, 2014 at 04:36 AM
check whether the object is ur playerObject tag..
void OnCollisionEnter(Collision collision) {
if (collision.gameObject.tag == "makeAtag..PutItHere.."){
//tele to lvl
}
}
or
void OnTriggerEnter(Collider collider) {
if (collider.gameObject.tag == "makeAtag..PutItHere.."){
//tele to lvl
}
}
if you want to pass through it.
Answer by VIPINSIRWANI · May 07, 2014 at 07:21 AM
For Coin you should use OnTriggerEnter
void OnTriggerEnter(Collider cldr)
{
if(cldr.GameObject.tag == "coin")
{
Debug.log("Perform Your Action...");
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613144426im_/https://answers.unity.com/themes/thub/images/avi.jpg)