- Home /
Floor to damage player
Hello,
I'm trying to make a floor which slows, damage, etc. the player when the player moves on top of it.. my player is a Rigidbody with 2D collision on, and the floor should be without collision ..
im not sure how would i communicate between the two object since one has a collision and the other is not, so i though maybe there is a way to check if two rigidbodies are on top of each other?
im quit not sure how would i check this condition tried to search online but i didn't know what should i search for so.. :D
sorry if this is very basic but yeh ^^
Best Regards, MrHero
Well I don't use 2D myself but if you put a collider on that section of floor and tick IsTrigger then it'll affect OnTriggerEnter (might be slightly different for a 2D collider) but not actually cause a collision so will not stop the player. You can detect that and apply whatever you want to the player.
@$$anonymous$$mmpies Thanks that really help in getting the since of what i should do ^^
Answer by BlackWingsCorp · Dec 26, 2014 at 11:36 PM
You can put a box collider on the floor and check the is Trigger option, make sure the trigger area is high enough so that it can detect the player, add a tag "Player" to the player character, then use something like this:
public int health = 100;
public float speed = 20.0f;
public bool isIn = false;
void Update(){
if(isIn)
health--;
speed = speed/2;
}
void OnTriggerEnter2D(Collider2D enter){
if(enter.gameobject.tag == "Player"){
isIn = true;
}
}
void OnTriggerExti2D(Collider2D exit){
if(exit.gameobject.tag == "Player"){
isIn = false;
}
}
Hope it helps
@BlackWingsCorp thanks that really helped, it worked as needed ^-^
Your answer