My raycast target enemy works perfect, my only problem is when I have a target the only way to untarget is to kill the enemy or to right click. I want to make it so if I have a target and i choose to click another one, the target will change. Cant figure it out!
void Update () {
if( Input.GetMouseButtonDown(0) && newon == true)
{
Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
RaycastHit hit;
if( Physics.Raycast( ray, out hit, 100 ) )
{
float distance = Vector3.Distance(hit.transform.position, transform.position);
if(distance < 8) {
string hitTag = hit.transform.tag;
if (hitTag == "enemy" && newon == true)
{
Debug.Log( hit.transform.gameObject.name );
target = hit.transform.gameObject;
SelectTarget();
}
}
}
}
if (Input.GetMouseButtonDown (1)) {
DeselectTarget();
}