Hello all,I’m a newbie to Unity. So please help me with this.
My 2D game object(zombie) needs to start functioning only when the player is at a specific distance.
The functioning is as follows:
The game object must animate first and then only follow the player continuously after. It must face the player at all times and not stop functioning even if he leaves its vicinity.
I wrote the following piece of code which won’t work.
public class ZombieFollow : MonoBehaviour
{
private Animator zAnim;
public Transform target;
void Start()
{
zAnim = GetComponent < Animator > ();
}
void Update()
{
//[WORKING]Animates when the player is close
if (Vector3.Distance(transform.position, target.position) < 3f)
{
zAnim.SetTrigger(“appear”);
zAnim.SetTrigger(“walk”);
}
//[NEED CODE]must wait first for the animation to complete
transform.lookAt(target);//[NOT WORKING]Makes the zombie invisible
//[NEED CODE]Follow the player and attack if collided
}
private void OnCollisionEnter2D(Collision other)
{
if(other.gameObject.tag==“Player”)
{
zAnim.SetTrigger(“attack”);
//[NEED CODE]wait again for animation to complete
Destroy(other.gameObject);
}
}
}