Hey,
I'm trying stop an enemy character to stop right in front of me and start attacking, but every time he gets in front of me he keeps walking past me or disappears from in front of me to the right of me when i put the distance where i want him to stop. Is there a way i can make the character controller come to a complete stop at a certain distance?
var attackDistance = 17.0;
while (true)
{
yield WaitForSeconds(0.2);
var offset = transform.position - target.position;
//This where i want it to stop...
if (offset.magnitude < attackDistance){
var StopDirection = characterController.transform.InverseTransformDirection(Vector3(50,0,50));
characterController.SimpleMove(target.position);
animation.Play("attack");
yield;
//return;
}
function RotateTowardsPosition (targetPos : Vector3, rotateSpeed : float) : float
{
// Compute relative point and get the angle towards it
var relative = transform.InverseTransformPoint(targetPos);
var angle = Mathf.Atan2 (relative.x, relative.z) * Mathf.Rad2Deg;
// Clamp it with the max rotation speed
var maxRotation = rotateSpeed * Time.deltaTime;
var clampedAngle = Mathf.Clamp(angle, -maxRotation, maxRotation);
// Rotate
transform.Rotate(0, clampedAngle, 0);
// Return the current angle
return angle;
}
animation.Play("walk");
// First we wait for a bit so the player can prepare while we turn around
// As we near an angle of 0, we will begin to move
var angle : float;
angle = 180.0;
var time : float;
time = 0.0;
var direction : Vector3;
while (angle > 5 || time < attackTurnTime)
{
time += Time.deltaTime;
angle = Mathf.Abs(RotateTowardsPosition(target.position, rotateSpeed));
move = Mathf.Clamp01((90 - angle) / 90);
// depending on the angle, start moving
animation["walk"].weight = animation["walk"].speed = move;
direction = transform.TransformDirection(Vector3.forward * attackSpeed * move);
characterController.SimpleMove(direction);
yield;
var timer = 0.0;
var lostSight = false;
while (timer < extraRunTime)
{
angle = RotateTowardsPosition(target.position, attackRotateSpeed);
// The angle of our forward direction and the player position is larger than 50 degrees
// That means he is out of sight
if (Mathf.Abs(angle) > 40)
lostSight = true;
// If we lost sight then we keep running for some more time (extraRunTime).
// then stop attacking
if (lostSight)
timer += Time.deltaTime;
// Just move forward at constant speed
direction = transform.TransformDirection(Vector3.forward * attackSpeed);
characterController.SimpleMove(direction);
}
asked
Apr 03 '10 at 10:41 PM
dreal
45
●
6
●
6
●
12