I’m making a jackhammer. I can’t figure out how to make it where when the jackhammer hits the rock it changes a variable. Please help. (By the way I’m a beginner so I’m sorry if this seeems simple. :D)
1 Answer
1You can use OnCollisionEnter to detect when the jackhammer hits the rock (NOTE: at least one of them must have a rigidbody attached). Place this code in the jackhammer script:
function OnCollisionEnter(col: Collision){
if (col.transform.name == "Rock"){ // you could compare tag as well
// do whatever you want when the jackhammer hits the rock:
rockHit = true; // like setting a boolean variable, for instance
}
}
Ok, if the jackhammer is a trigger, you must use OnTriggerEnter: <pre> function OnTriggerEnter(col: Collider){ if (col.tag == "Rocks"){ col.renderer.enabled = false; } } </pre> This event passes a Collider instead of a Collision, and the Collider has direct access to all "inherited variables" like tag, name, renderer etc.
– aldonalettoBut noticed that continuous collision detection is much more expensive than discrete detection. If there are lots of rigidbodies using continuous detection, your frame rate can become ridiculous.
– aldonaletto