So I am currently Trying to create a zombies game, where I have a script written that would enable a hitting animation if it was "close" to the player. The code for this script is below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class ZombieNavMesh : MonoBehaviour
{
private Transform movePositionTransform;
private NavMeshAgent navMeshAgent;
private Animator animator;
static bool close;
private void Start()
{
movePositionTransform = GameObject.Find("Player").transform;
animator = GetComponent<Animator>();
}
private void Awake()
{
navMeshAgent = GetComponent<NavMeshAgent>();
}
private void Update()
{
if (!close)
{
navMeshAgent.destination = movePositionTransform.position;
animator.SetBool("Close", false);
}
if (close)
{
animator.SetBool("Close", true);
}
}
public void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{
Debug.Log("Should hit");
close = true;
}
}
public void OnTriggerExit(Collider other)
{
if(other.tag == "Player")
{
Debug.Log("Should not hit");
close = false;
}
}
}
The problem in here is that, if one zombie Enters Trigger, and another zombie Exit Trigger, the bool value close would be set to false, so both would perform walking animation; it should be the "close" zombie hitting animation, and the far zombie walking animation.
CodePudding user response:
Remove the word static
from static bool close;
static
means there is only one of that thing shared by all versions and instances of the class.