- Home /
Player Collide with Fast moving Rigidbody to cause Death?
I was wondering If anybody could help me;
I need to know how i would go about making a player die if it gets hit by a fast moving Rigidbody. I need the script and the collider trigger to be attached to the player, so i dont know how i could check that the velocity of the rigidbody is enough to kill the player.
Answer by DESTRUKTORR · Aug 31, 2013 at 10:01 PM
// C# version
void OnCollisionEnter(Collision col)
{
if(col.relativeVelocity > someValue)// where "someValue" is a variable of your choosing.
{
killPlayer();// You'll have to implement how the player will die, yourself, but just call the method(s) needed to kill them, here.
}
}
Or, if you use JavaScript
// JavaScript version
function OnCollisionEnter(collision : Collision)
{
if(col.relativeVelocity > someValue)// where "someValue" is a variable of your choosing.
{
killPlayer();// You'll have to implement how the player will die, yourself, but just call the function(s) needed to kill them, here.
}
}
Put one of these in a script on your character (choose whichever you like. Both do the same thing).
Hey, thanks for answering. I think you are right, but I get an error message saying "$$anonymous$$ identifier: col". do you know what is happening, or what to replace "col" with?
Okay, I think I fixed my problem, but when I run my game, nothing happens when the player is hit. Do you know what the problem could be?
What does your kill method look like? Also, try putting this:
Debug.Log("It's registering the hit");
in that method, just after the call to killPlayer();
If it is registering the collision, it will print "It's registering the hit" to the console. If not, it won't, and you'll have found the problem (though it may not be the source of the problem, it is definitely a step in the right direction).
If it does print that, then the issue is likely with your kill method.
When I said nothing happens I meant that Debug.Log("You are Dead") didn't show up. It's all good now though, I fixed it. Thanks for your first answer!
Your answer
Follow this Question
Related Questions
rigidbody.velocity not working on x axis 2 Answers
Velocity powered rigidbody on a moving platform without parenting. 3 Answers
Rigidbody player movement unrealistic velocity problem. 1 Answer
How to slow player on x and z 1 Answer
Addforce up to kenemetic rigidbody of the triggerobject player 2 Answers