- Home /
How do I get two objects (both with rigidbody and colliders) to pass through each other (and only each other)?
Hi friends!
I am making a 2d side scrolling game. It is two player locally, and I obviously need both players to collide with their environment, so that they can't walk through walls and such. This was easy to accomplish, but now I have hit a snag. The players, both having rigidbody and colliders, run into each other. I want them to be able to pass through each other so that the players can pass each other. How can I make it so that the players still collide with the floor and walls, but can pass through each other as if they didn't have colliders or rigidbody? Any help is greatly appreciated!
Answer by Jamora · Jun 01, 2013 at 09:00 AM
This can be achieved with physics layers. Just put your players on different layers, then use http://docs.unity3d.com/Documentation/ScriptReference/Physics.IgnoreLayerCollision.html to tell Unity to not collide them with eachother.
Answer by Supershandy · May 31, 2013 at 08:37 PM
Where you use OnCollisionEnter or OnTriggerEnter, just specify which items it can't pass through using tags.
i.e
Tag "Wall" with "Wall" in Inspector
Then in player code....
var velocity : float = "Something";
function OnTriggerEnter/OnCollisionEnter ( col : Collider )
{ if (GameObject.FindWithTag("wall")} { velocity = 0; )
}
That example will mean that if it hits something with the tag "wall" then it can't pass through it. Anything else it will pass through.
Hope this has been of some help and help you move in the right direction.
Wait, this confuses me a little bit. What should i put in place of "something"? The starting velocity? This doesn't seem to work for me... Forgive my ignorance.
where it says "Something" means just to put whatever value you want in there i.e var velocity : float = 1.5;
How do you mean it doesn't work? How are you working out your collisions?
Your answer
Follow this Question
Related Questions
Dealing with 2D movement and collisions! 1 Answer
Continuously moving rigidbody 2 Answers
My 2 Objescts doesnt collide 1 Answer