Hi, I’m recent starting to use Unity and C# and I have a little issue. That issue is that when an enemy deals damage to the player it counts the damage of all enemies on the screen, as if they where hitting you at the same time. I hope someone can help me soon as possible, I’ll paste and upload my script to the thread so someone can help me to correct it. (Sorry for my English, it’s a bit bad)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class IA_Enemy : MonoBehaviour
{
public Transform player;
UnityEngine.AI.NavMeshAgent enemyNav;
private bool inside = false;
public Animator enemyAnim;
public GameObject self;
public PlayerManagement PlayerManagement;
void Start()
{
enemyNav = GetComponent<UnityEngine.AI.NavMeshAgent>();
enemyAnim = this.GetComponent<Animator>();
}
void OnTriggerStay(Collider other)
{
if (other.tag == ("Player"))
{
inside = true;
if (PlayerManagement.dmgTemp > 1.2f)
{
PlayerManagement.dmgTemp = 0;
}
}
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == ("Player") && PlayerManagement.dmgTemp > 1.2)
{
PlayerManagement.dmgTemp = 0;
PlayerManagement.cameraAnim.Play("CameraHitAnim");
enemyNav.destination = this.transform.position;
}
}
void OnTriggerExit(Collider other)
{
if(other.tag == ("Player"))
{
inside = false;
}
}
void Update()
{
if (PlayerManagement.dmgTemp < 0.2f)
{
PlayerManagement.ActualLife -= 1f;
enemyAnim.Play("Attack");
enemyNav.destination = this.transform.position;
}
if (!inside && PlayerManagement.dmgTemp > 0.8f)
{
enemyNav.destination = player.position;
}
if (inside)
{
enemyNav.destination = this.transform.position;
}
var lookPos = player.position - transform.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(lookPos);
self.transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime);
}
}
7937596–1014694–IA_Enemy.cs (1.93 KB)