I have this code that, when the Enemy hits the player's hitbox, attack trigger equals 1 and then it moves away from the player. I have this part in an if statement.
What I am trying to do is when the enemy gets shot, it also moves away from the player.
The variable that checks if the enemy has been hit is "IsItHit".
IsItHit = 1 (Enemy has been hit)
I have tried writing "if ((AttackTrigger == 1) || (IsItHit == 1))" to check if attack trigger or IsItHit equal to 1 to activate the move away code but it does not work.
How would I solve this problem. Thanks
using UnityEngine;
using UnityEngine.AI;
[RequireComponent(typeof(NavMeshAgent))]
public class EnemyMovementWithDamage : MonoBehaviour {
public GameObject ThePlayer;
// public float TargetDistance;
public float AllowedDistance = 2;
public GameObject TheEnemy;
public int AttackTrigger;
public RaycastHit Shot;
private NavMeshAgent agent;
private NavMeshPath path;
public float goalDistance = 100;
public int runaway = 0;
public float distance;
public int IsItHit;
void Start() {
path = new NavMeshPath();
agent = GetComponent<NavMeshAgent>();
}
void Update() {
IsItHit = Shootable.IsHit;
if (distance >= 40){
runaway = 0;
Shootable.IsHit=0;
Debug.Log("runaway = 0");
}
transform.LookAt (ThePlayer.transform);
//if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), out Shot)) {
//TargetDistance = Shot.distance;
distance =Vector3.Distance(TheEnemy.transform.position,ThePlayer.transform.position);
//}
if (runaway == 0) {
if (AttackTrigger ==0){
GetComponent<NavMeshAgent>().speed = (7);
agent.SetDestination(ThePlayer.transform.position);
Debug.Log("Moving towards player");
}
}
if (AttackTrigger == 1) {
Debug.Log("hit");
runaway = 1;
Debug.Log("runaway = 1");
GetComponent<NavMeshAgent>().speed = (50);
Vector3 target = transform.position + Vector3.forward * goalDistance;
agent.SetDestination(target);
}
}
void OnTriggerEnter() {
AttackTrigger = 1;
}
void OnTriggerExit() {
AttackTrigger = 0;
}
}
Aucun commentaire:
Enregistrer un commentaire