- Home /
This question was
closed Jan 07, 2014 at 11:43 PM by
AlucardJay for the following reason:
Other : Inappropriate
Question by
get52 · Dec 27, 2013 at 04:44 PM ·
collisionplatformerdetectionspike
Trying to figure out how to detect when the player hits a spike
I used rigid body on the player with rotation locked and the spike is a box with boxcollider attached and this is a 2D game if that matters, I've google'd it found some threads with similar questions but the shit wasnt working at all please help me.
Comment
Best Answer
Answer by Bmarlyman21 · Dec 27, 2013 at 05:01 PM
use a trigger collider and put into script OnTriggerEnter .
function OnTriggerEnter( touching : Collider ){
if(touching.gameObject.tag == "spike")
{
print("hit");
}
}
Sadly this isnt working i set the spike to trigger and i keep collideing the player into the trigger and nothing prints in console.
Answer by baris150490 · Dec 27, 2013 at 05:23 PM
Hi, you can use bounds.Intersects.Like the following;
var playerBounds : Bounds;
var spikeBounds : Bounds;
var player : Transform;
var spike : Transform;
function Start ()
{
playerBounds = player.collider.bounds;
spikeBounds = spike.collider.bounds;
}
function Update ()
{
if(playerBounds.Intersects(spikeBounds))
{
//You hit it!
}
}