- Home /
Question by
Jirogirg · Oct 14, 2013 at 11:53 PM ·
movementlerpzsimplemove
Problem with enemy moving onto player (Lerpz tutorial)
I'm working on this - https://www.assetstore.unity3d.com/#/content/5087
When player is close to enemy it should start moving. But it's not. I think the problem in simplemove. Because for one frame it moves, but then stops. And not moving until program enters in "while" cycle again. What can be wrong?
function Attack ()
{
isAttacking = true;
if (attackSound)
{
if (audio.clip != attackSound)
{
audio.Stop(); // stop the idling audio so we can switch out the audio clip.
audio.clip = attackSound;
audio.loop = true; // change the clip, then play
audio.Play();
}
}
// Already queue up the attack run animation but set it's blend wieght to 0
// it gets blended in later
// it is looping so it will keep playing until we stop it.
animation.Play("attackrun");
// 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["attackrun"].weight = animation["attackrun"].speed = move;
direction = transform.TransformDirection(Vector3.forward);// * attackSpeed * move);
characterController.SimpleMove(direction * attackSpeed * move);
yield;
}
// Run towards player
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);
// Keep looking if we are hitting our target
// If we are, knock them out of the way dealing damage
var pos = transform.TransformPoint(punchPosition);
if(Time.time > lastPunchTime + 0.3 && (pos - target.position).magnitude < punchRadius)
{//3
// deal damage
target.SendMessage("ApplyDamage", damage);
// knock the player back and to the side
var slamDirection = transform.InverseTransformDirection(target.position - transform.position);
slamDirection.y = 0;
slamDirection.z = 1;
if (slamDirection.x >= 0)
slamDirection.x = 1;
else
slamDirection.x = -1;
target.SendMessage("Slam", transform.TransformDirection(slamDirection));
lastPunchTime = Time.time;
}
// We are not actually moving forward.
// This probably means we ran into a wall or something. Stop attacking the player.
if (characterController.velocity.magnitude < attackSpeed * 0.3)
break;
// yield for one frame
yield;
}
isAttacking = false;
// Now we can go back to playing the idle animation
animation.CrossFade("idle");
}
Comment