In my game scenario , i have two sphere game object. In that i have one sphere game object is on statis position and other sphere game object is used as bullet. For that i have created prefab of sphere game object. And i have put empty game object in my 2D scene and instantiate my bullet prefabe on that empty game object position.
Now when i shoot bullet i want to detect collision. I have tried so many methods but none of them works for me.
My code:
function Update ()
{
var spawnPoint : GameObject = GameObject.Find("Bullet");
if(Input.GetMouseButtonDown(0))
{
if(spawnPoint != null)
{
var hit : RaycastHit ;
var ray : Ray = Camera .main .ScreenPointToRay (Input .mousePosition );
if(Physics.Raycast (ray, hit))
{
shootProjectile = Instantiate(bulletSphere, spawnPoint.transform.position,spawnPoint.transform.rotation);
shootProjectile.rigidbody.useGravity = false;
var tempx = hit.transform.position.x;
var tempy = hit.transform.position.y;
shootProjectile.rigidbody.AddForce (shootProjectile.transform.TransformDirection(new Vector3(tempx,tempy,0) * 8000 * Time.deltaTime));
//if(hit.collider.gameObject.tag == "Enemy")
// {
// Destroy(hit.collider.gameObject);
// }
}
}
}
}
Method i have tried :
OnControllerColliderHit (hit : ControllerColliderHit)
OnCollisionEnter(other : Collision )
Which collision matrix do i need to use?
please please help me to solve my this problem. Thanks in advance.