void Update() { if (Input.GetMouseButtonDown(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { if (hit.collider.tag == "Mole") { Instantiate(fx_Punch, hit.point, Quaternion.identity); MoleBehavior mole = hit.collider.gameObject.GetComponent <MoleBehavior>(); mole.SwitchCollider(0); mole.anim.SetTrigger("hit"); //Debug.Log(hit.collider.gameObject + " got hit"); } } } }
void Update() { if (Input.GetMouseButtonDown(0)) //0:left mouse button, 1:right mouse button 2:middle { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //creat ray //Ray ScreenPointToRay(Vector3 position) : Return a ray going from camera through a screen point. RaycastHit hit; if (Physics.Raycast(ray, out hit))//physics check { if (hit.collider.tag == "Mole") { MoleBehavior mole = hit.collider.gameObject.GetComponent <MoleBehavior>(); //get the MoleBehavior from the Mole, that i hit mole.SwitchCollider(0); //turning off mole.anim.SetTrigger("hit"); //using Trigger for calling animation //Debug.Log(hit.collider.gameObject + " got hit ");//check what we hit Mole } } } }