Hello, I am having a frustrating time figuring out what is the problem with the script I put together by going into the documentation for Move.Toward. It doesn’t seem to be working because the enemies are standing completely still even though the debugger explicitly shows that the compiler is in fact going through it no problem. What I’m trying to do is make the enemies chase the player, for now.
UPDATE: One person pointed out that the code transform.parent = plane.transform; is suspected of messing things up so please take a look and see if it looks off in its context.
I would really appreciate it if someone were to help or advise me how I should go about this.
Here is the code that I am working on which I hope someone could help point out the problem:
using UnityEngine;
using System.Collections;
public class EnemyAIMelee : MonoBehaviour
{
public float targetDistance = 20.0f;
public float attackDistance = 10.0f;
public float enemySpeed = -40.0f;
public float chaseSpeed = 1.0f;
private bool spawningIn = true;
private bool unlockAI = false;
public GameObject playerCharacterManager;
void Start()
{
playerCharacterManager = GameObject.FindGameObjectWithTag ("PlayerCharacterManager");
}
// Update is called once per frame
void Update ()
{
SpawnIn ();
MovingAI ();
}
void SpawnIn()
{
if (spawningIn == true)
{
GameObject plane = GameObject.FindGameObjectWithTag("PlayerPlane");
if (transform.position.z - plane.transform.position.z <= targetDistance)
{
Debug.Log ("Made it to the playing field");
SendMessage("EnemyMovingAnimation", false);
transform.parent = plane.transform;
unlockAI=true;
spawningIn = false;
}
else
{
SendMessage("EnemyMovingAnimation", true);
transform.position += transform.forward*enemySpeed*Time.deltaTime;
}
}
}
void MovingAI()
{
if(unlockAI == true)
{
float chasingSpeed = chaseSpeed * Time.deltaTime;
transform.LookAt(2 * transform.position - playerCharacterManager.transform.position);
//This line of code is causing me the problem
transform.position = Vector3.MoveTowards (transform.position, playerCharacterManager.transform.position, chasingSpeed);
Debug.Log ("MovingAI has been reached");
SendMessage ("EnemyMovingAnimation", true);
float dist = Vector3.Distance(playerCharacterManager.transform.position, transform.position);
if(dist<=attackDistance)
{
AttackAI();
}
}
}
void AttackAI()
{
unlockAI =false;
SendMessage ("EnemyAttackAnimation", true);
Debug.Log ("AttackAI has been reached");
WaitTime();
}
void WaitTime()
{
unlockAI =true;
Invoke ("MovingAI",2.0f);
}
}