I have this enemy move script in javascript:
var ThePlayer : GameObject;
var TheEnemy : GameObject;
var EnemySpeed : float;
var MoveTrigger : int;
function Update () {
if (MoveTrigger == 1) {
EnemySpeed = 0.02;
transform.position = Vector3.MoveTowards(transform.position, ThePlayer.transform.position, EnemySpeed);
}
}
And (if it has anything to do with this) this enemy follow script in C#:
using UnityEngine;
using System.Collections;
public class ZombieFollow : MonoBehaviour
{
public GameObject ThePlayer;
public float TargetDistance;
public float AllowedRange = 10;
public GameObject TheEnemy;
public float EnemySpeed;
public int AttackTrigger;
public RaycastHit Shot;
public int IsAttacking;
public GameObject ScreenFlash;
public AudioSource Hurt01;
public AudioSource Hurt02;
public AudioSource Hurt03;
public AudioSource Hurt04;
public int PainSound;
void Update()
{
transform.LookAt(ThePlayer.transform);
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Shot))
{
TargetDistance = Shot.distance;
if (TargetDistance < AllowedRange)
{
EnemySpeed = 0.025f;
if (AttackTrigger == 0)
{
TheEnemy.GetComponent().Play("Walking");
transform.position = Vector3.MoveTowards(transform.position, ThePlayer.transform.position, EnemySpeed);
}
}
else
{
EnemySpeed = 0;
TheEnemy.GetComponent().Play("Idle");
}
}
if (AttackTrigger == 1)
{
if (IsAttacking == 0)
{
StartCoroutine(EnemyDamage());
}
EnemySpeed = 0;
TheEnemy.GetComponent().Play("Attacking");
}
}
void OnTriggerEnter()
{
AttackTrigger = 1;
}
void OnTriggerExit()
{
AttackTrigger = 0;
}
IEnumerator EnemyDamage()
{
IsAttacking = 1;
PainSound = Random.Range(1, 5);
yield return new WaitForSeconds(0.35f);
ScreenFlash.SetActive(true);
GlobalHealth.PlayerHealth -= 1;
if (PainSound == 1)
{
Hurt01.Play();
}
if (PainSound == 2)
{
Hurt02.Play();
}
if (PainSound == 3)
{
Hurt03.Play();
}
if (PainSound == 4)
{
Hurt04.Play();
}
yield return new WaitForSeconds(0.05f);
ScreenFlash.SetActive(false);
yield return new WaitForSeconds(1.10f);
IsAttacking = 0;
}
}
that make my enemy move towards the player, but it often runs into walls and objects. Since I want him to go around these things, I added a navmesh agent, but it messes up the enemy follow script. The enemy flips around and moves towards the player extremely fast. When I set the navmesh speed to 0, it moves towards the player and then it moves away. What am I doing wrong?
↧