- Home /
Simple Collision Detection
In my seen I have Terrine, First person controller and one cube with rigidbody. I want to learn that how can i hide or destroy cube when First person controller collide with cube. Thankyou
Answer by AnaRhisT · Jun 21, 2010 at 05:14 PM
JS Version.
var Cube : Transform;
function OnControllerColliderHit (hit : ControllerColliderHit)
{
if(hit.gameObject.tag == "cube")Destroy(Cube);
}
You add this script ot the first person controller.
Thank you. I added this script to first person controller. but its not working. :(. if you have source file then plz send me on aamir.rafiq@gmail.com. Regards
Because u didn't create a tag to the cube's gameobject. Create a new tag called "cube", and then make it the tag of ur Cube's gameobject. simple :)
Answer by Pirate___man · Jun 21, 2010 at 05:14 PM
Here is a c# script that I used to trigger a particle system effect and remove the object from the scene. With a little bit of tweaking you can make it so that it doesn't trigger the particle effect and just vanishes. Than again you might want it to go boom. Your choice. (If you do want it to explode there is a nice explosion particle effect in the standard asset/particle section.)
using UnityEngine; using System.Collections;
public class OnCollideExplode : MonoBehaviour {
// A grenade // - instantiates a explosion prefab when hitting a surface // - then destroys itself public GameObject explosionPrefab; public float explodeSecs = -1;
void Awake() { if(explodeSecs > -1) Invoke ("DestroyNow", explodeSecs); }
void OnCollisionEnter( Collision collision ) { // Rotate the object so that the y-axis faces along the normal of the surface ContactPoint contact = collision.contacts[0]; Quaternion rot = Quaternion.FromToRotation(Vector3.up, contact.normal); Vector3 pos = contact.point; Instantiate(explosionPrefab, pos, rot); // Destroy the projectile Destroy (gameObject); }
void DestroyNow() { Instantiate(explosionPrefab, transform.position, transform.rotation); Destroy (gameObject); }
}
It's with CollisionEnter, he needs to check if the gameobject performed a movement, which means you need to make it OnControllerColliderHit.