- Home /
SimpleMove with Chasing
I am trying to have the enemy chase the player. I am using SimpleMove with the enemy. Of course, if you do not use
"characterController.SimpleMove ();"
the collisions will not work.
How do I set the enemy to chase a player with that function with collision?
I do not want to use a rigidbody.
This is the code I use to attack, but the enemy goes right into the player even though the player has a charactercontroller. I move the character and he pops out and the collision works...?
transform.position = Vector3.Lerp ( transform.position, target.position, attackSpeed * Time.deltaTime );
Note: When I push the character up against the enemy, the collision works, but when I move the enemy against the player, it doesn't, why is that?
Thanks!
Answer by aldonaletto · Jul 19, 2012 at 02:53 AM
Chasing the player is the easiest part: just rotate the enemy in the player's horizontal direction and make it move forward. But any AI must do more than this: usually the enemy must stay in an idle state when the player is too far or not visible, doing nothing or walking on a path defined by waypoints; when the player enters the chase range and/or becomes visible, chase him; when reaching a minimum distance from the player (attack range), stop and attack him. The basic script is as follows:
var speed: float = 4.5; var turnSpeed: float = 5; var chaseRange: float = 80; var attackRange: float = 2.5; private var target: GameObject; private var controller: CharacterController;
function Start(){ controller = GetComponent(CharacterController); // cache the CharacterController target = GameObject.FindWithTag("Player"); // make sure the player is tagged "Player" }
function Update(){ // find the player direction: var dir = target.transform.position-transform.position; dir.y = 0; // make it strictly horizontal var dist = dir.magnitude; // measure the distance if (dist > chaseRange){ // player too far: // idle state: do nothing or make the enemy follow a path // based on waypoints } else if (dist > attackRange){ // player in chase range but still out of attackRange: // chase state: var rot = Quaternion.LookRotation(dir); // find target direction... // and rotate gradually to it transform.rotation = Quaternion.Slerp(transform.rotation, rot, turnSpeed*Time.deltaTime); controller.SimpleMove(transform.forward*speed); // chase him! } else { // attack state: // attack the player } } You can start with this basic script and gradually implement the idle and attack modes.
NOTE: About your last question: the CharacterController only check collisions when it's moving with Move or SimpleMove; if you move it by altering its position (setting transform.position directly) or move any other object over it, collisions are ignored.
Thanks man! I really appreciate you and this community! I hope one day I will be good enough at program$$anonymous$$g to help others...but that will be awhile =/
Thanks. I had issues with rigidbody and wasnt able to find a character controller after searching a lot. yours worked.