- Home /
Enemy follow player when player is not looking. OnBecameInvisible not working.
So i want to create a weeping angels effect on my enemies in my game. So when the player can see the enemy, they don't move and when the player can't see them, they move closer to the player. This is my code thats attached to the enemy and its not working. Would really appreciate some help!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeepingAngel : MonoBehaviour
{
public GameObject Player;
// Start is called before the first frame update
void Start()
{
Player = GameObject.FindGameObjectWithTag("Player");
}
void OnBecameInvisible()
{
if (Player)
{
transform.position = Player.transform.position - Player.transform.forward;
Vector3 lookPos = Player.transform.position - transform.position;
lookPos.y = 0;
transform.rotation = Quaternion.LookRotation(lookPos);
}
}
}
Answer by VARUN88 · Apr 11, 2021 at 07:11 PM
You are not calling the method in start or update function and use if(Player != null) .
Your answer
Follow this Question
Related Questions
Visual Studio for Mac cannot save Unity C# script after edited it. 0 Answers
Problem with code writing and maybe inheritance in visual studio! 1 Answer
Using UWP Universal Windows Platform API in Unity 2 Answers
Billboarding in Unity 5 with Locked Z-Axis 0 Answers
Is there any way to restore previous save of C# script 0 Answers