lock on and unlocking [3D]
So I wrote the following to have my character face and "lockon" to an enemy when you right click on it
//Enemy Detect Ray
Ray cameraRay = mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit rayHit = new RaycastHit();
if (Physics.Raycast(cameraRay, out rayHit))
{
//Detect Lockon
//Lock
if (rayHit.collider.tag == "Enemy" && Input.GetMouseButtonDown(1))
{
lockedEnemy = rayHit.transform.gameObject;
Debug.Log("I've Lockedon to " + lockedEnemy);
isLockedOn = true;
}
//Unlock
if (isLockedOn == true && Input.GetMouseButton(1) || lockedEnemy == null)
{
Debug.Log("Breaking Lockon");
isLockedOn = false;
lockedEnemy = null;
}
}
I tried to create a way you could "unlock" from an enemy or possibly lock onto a different one but the issue I'm having is that when the unlock is enabled, in game when I lock on, it will instantly unlock. What am I doing wrong?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612160050im_/https://answers.unity.com/themes/thub/images/avi.jpg)