- Home /
Enemy AI character controller movement is super slow
I cant figure out why the character controller on my enemy AI is moving super slow. I put a debug statement in the chase function to see what was happening. The controller is moving at (0.0,0.0,-0.1), even though the movementDirection vector is < or > 0 for x,y & z. It changes based on my postion, so the character at least turns to face me. When I wrote this in js it worked fine. In c# its giving me all kinds of trouble. The code is a hodge podge of my code and some from a tutorial I was following. I have spent a few hours researching and trying different things. Thanks in advance for the help.
using UnityEngine;
using System.Collections;
public class AISimple : MonoBehaviour {
public float Distance;
public Transform Target;
public float lookAtDistance= 25.0f;
public float chaseRange= 15.0f;
public float moveSpeed= 5.0f;
public float Damping= 6.0f;
public float fov= 160.0f;
private RaycastHit hit;
public CharacterController controller;
public float gravity = 20.0f;
private Vector3 moveDirection = Vector3.zero;
/*
* Is player in line of sight?
*/
bool LineOfSight ( Transform target ){
if (Vector3.Angle(target.position - transform.position, transform.forward) <= fov &&
Physics.Linecast(transform.position, target.position, out hit) &&
hit.collider.transform == target) {
return true;
}
return false;
}
/*
This ai will fly and move through objects inlcuding terrain!
*/
void Update (){
if (LineOfSight(Target) == true && hit.transform == Target) // Target is player
{
//enemy sees you - perform some action
// Gauge the distance to the player. Line in 3d space. Draws a line from source to Target.
Distance = Vector3.Distance(Target.position, transform.position);
// Turn yellow enemy is getting close. Will chase soon.
if (Distance < lookAtDistance)
{
lookAt();
}
// Safe distance
if (Distance > lookAtDistance)
{
renderer.material.color = Color.green;
}
// Attack! Chase the player until/if player leaves attack range.
if (Distance < chaseRange)
{Debug.Log("enemy chase");
chase ();
}
}
else
{
//enemy doesn't see you
}
}
// Turn to face the player.
void lookAt (){
renderer.material.color = Color.yellow;
// Rotate to look at player.
Quaternion rotation= Quaternion.LookRotation(Target.position - transform.position);
// Dampening will slow the turn speed of enemy.
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
// transform.LookAt(Target);
}
void attack (){
// in progress
}
void chase (){
renderer.material.color = Color.red;
moveDirection = transform.forward;
moveDirection *= moveSpeed;
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
Debug.Log(moveDirection * Time.deltaTime);
}
}
I tried, no luck. I will just sleep on it. $$anonymous$$aybe it will come to me over night. Thanks.
Yeah I use Simple$$anonymous$$ove you use $$anonymous$$ove so it's a bit different
Answer by SudoSandwich · Aug 30, 2013 at 05:37 PM
Figured it out! The code is fine. My enemy was running into his own face! I removed all colliders except the character collider and it all works fine now.
I loled for real when I read this, glad you found the problem.
Exactly the same problem I was having except $$anonymous$$e liked walking sideways. Thanks.