- Home /
Crushing Character
I am making a platform game, where the player can get crushed if the moving platform collides on the player when the player is on the floor, So i am using a code
function OnCollisionEnter(collision : Collision)
{
if((collision.gameObject.name == "Crushed Death") && (collision.gameObject.name == "Moving Platform"))
{
Destroy(obj.gameObject);
}
}
function Update()
{
}
The code doesnt seem to be working!! And I attach this script to the player!
Answer by wccrawford · Nov 21, 2011 at 03:06 PM
The name of the object cannot be more than 1 thing at a time, so your code can never evaluate to true.
I don't think you are right(please correct me if I am wrong), since the name is a string, it will just look for a string matching it regardless of length and such.
I may be wrong, though.
Is there any way I can achieve the crush mechanic?
@Legion He has checked that collision.gameObject.name equals both "Crushed Death" and "$$anonymous$$oving Platform". Since it cannot be both things at once, it can't work.
@hariprasadh10792 What you're looking to do is check that you're touching both the platform and the whatever is crushing the player from above. How you do that will depend on your other code. If you're using the built-in character controller and physics, you can probably checked for a collision with the crushing thing, and check if the isGrounded flag for the character controller.
Answer by FLASHDENMARK · Nov 21, 2011 at 03:48 PM
function OnCollisionEnter(collision : Collision) {
if(collision.gameObject.name == "Crushed Death" && collision.gameObject.name == "Moving Platform"){
Destroy(gameObject);
}
}
There were a few things that may be wrong.
(Disclaimer: the method you used may be working right, but there is no really need for it). When you clarified your if statement you used double brackets E.G if((blahh) && (blah)). There is no need for "double bracketing" in your case. You could simply just write if(blah && blah)(with out the double brackets)
And what is "obj.gameObject"? Are you sure you are not supposed to use gameObject only?
Those were just suggestions that may be wrong. I cannot say for sure with the current information at hand.
obj is a var game object! So i wanted the script to mean something like if the obj is touching the floor and crushed death at the same time the obj gets destroyed!
and i also tried putting in the code you gave it doesnt seem to be working for some reason! The script also manages to compile without any errors!