- Home /
Problem with damaging game object
I am creating a game where you can drag around objects (kind like Amnesia). I want an object to break if it collides with another rigid body (with a velocity of 10 or faster). However, I am having problems "damaging: the object.
function OnCollisionEnter(collision : Collision) {
// Debug-draw all contact points and normals
for (var contact : ContactPoint in collision.contacts)
Debug.DrawRay(contact.point, contact.normal, Color.white);
// Play a sound if the coliding objects had a big impact.
if (collision.relativeVelocity.magnitude > 5)
audio.Play();
gameObject.broadcastMessage("applyDamage",100);
}
EDIT: I am new to coding, so I have "remixed" other people's code to achieve my goals.
Working Code Here
pragma strict
function OnCollisionEnter(collision : Collision) { // Debug-draw all contact points and normals for (var contact : ContactPoint in collision.contacts) Debug.DrawRay(contact.point, contact.normal, Color.white);
if(collision.relativeVelocity.magnitude > 5) { audio.Play(); gameObject.Broadcast$$anonymous$$essage("applyDamage", 100); } }
Answer by clunk47 · Dec 12, 2012 at 04:06 AM
Your if statement has more than 2 lines no? If both those conditions belong to 1 if statement you need to encapsulate.
if(collision.relativeVelocity.magnitude > 5)
{
audio.Play();
gameObject.broadacastMessage("applyDamage", 100);
}
otherwise, the only part that will be a part of that if statement would be audio.Play(), the first line.
Your answer
Follow this Question
Related Questions
Checking and destroying a rigidbody? 2 Answers
This might be simple but i'm lost. 4 Answers
Colliding with static model 1 Answer
How Can One Collider Recognize Contact With Another? 0 Answers
Damage with collision force? 0 Answers