Hi,
So I have a scene set up where the end goal is to get within range of the villain, look at them, and click to destroy them with the mouse (not necessarily with the mouse cursor pointed on them, just with the player character looking at them with the raycast). I have been able to set it up to where the enemy is destroyed on click no matter where the player is, however I want this to only occur once the ray is looking at them. Could anyone help me figure out how to create this code?
(I am also very new to coding so I am not familiar with all of the functions yet)
Hero code to cast the ray:
Ray r = new Ray();
r.origin = gameObject.transform.position;
r.direction = gameObject.transform.forward;
Debug.DrawLine(r.origin, r.origin + r.direction * 10.0f, Color.magenta);
RaycastHit rhit;
if (Physics.Raycast(r, out rhit, 10.0f))
{
if (rhit.collider.gameObject.tag == "Enemy")
{
gameObject.GetComponent<MeshRenderer>().material.color = Color.green;
/* if(Input.GetMouseButtonDown(0))
{
Destroy(gameObject);
}*/
}
else
{
gameObject.GetComponent<MeshRenderer>().material.color = Color.blue;
}
}
Villain code to be destroyed on click:
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Destroy(gameObject);
}
}