Good day all. I have an object with an enemy tag, it has a collider. When a player enters the collider, the enemy uses raycast to check for other objects if they do not exist, he uses LooAt .Debug.DrawRay works correctly and correctly indicates the target but the Targetplayer variable is not assigned. RayCast does not work the comparison does not occur and the variable is not assigned
public GameObject self;
public GameObject player;
public GameObject Targetplayer;
void Start () {
self = gameObject;
}
void Update () {
if(player){
RaycastHit hit1;
Vector3 playerDir = player.transform.position - self.transform.position;
Debug.DrawRay(transform.position, playerDir, Color.green);
Physics.Raycast(transform.position, playerDir, out hit1, Mathf.Infinity);
if(hit1.collider.tag == "Player")
{
Targetplayer = player;
}
}
if (Targetplayer){
transform.LookAt(player.transform);}
}
void OnTriggerEnter(Collider other){
if(other.CompareTag("Player")){
player = other.gameObject;
}
}
void OnTriggerExit(Collider other){
if(other.CompareTag("Player")){
player = null;
Targetplayer = null;
}
}
}