- Home /
Raycast collision detection on a 2d game
Hi everybody i have a problem, i am doing a 2d game where with mouse we must tap objects, i tried to use raycasts but does not Work here's the code:
void Start()
{
Ray ray=Camera.main.ScreenPointToRay(Input.moisePosition);
}
void ClickFunction()
{
If(Physics.Raycast(transform.position,-Vector3 up,out Ray,100))
{
Destroy(GameObject.FindObjectWithTag("zombie");
}
}
When raycast collides should destroy the gameobject but does not Work. What is wrong ? Thanks in advance!
Comment
Answer by HanClinto · Oct 06, 2013 at 02:55 AM
Try updating ray every time you click. Otherwise the only value used for your ray will be where the mouse was at when your level began.
I.E.:
void Start()
{
// This should be empty
}
void ClickFunction()
{
Ray ray=Camera.main.ScreenPointToRay(Input.mousePosition);
If(Physics.Raycast(transform.position,-Vector3 up,out Ray,100))
{
Destroy(GameObject.FindObjectWithTag("zombie");
}
}