Why are my enemies not moving after shaking?
I have an enemy using A* pathfinding to constantly move towards the player. I was working on a script that would cause them to 'vibrate' every time they were hit with a projectile. Problem is, often the enemy will no longer move after they finish shaking. It's like they won't stop lerping to the exact position they are stuck at or something, because they still retain all other functionality (they hurt to touch, receive damage and die, etc.).
Here is my code, can someone help me figure this out:
private float shakeAmount = 0f;
private EnemyAI enemyAIClone;
// Use this for initialization
void Awake()
{
enemyAIClone = GetComponent<EnemyAI>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
//Test the shake
StunShake (0.5f, .5f, true);
}
}
public void StunShake (float _amnt, float _leng, bool _isExplosion)
{
if (IsInvoking ("BeginShake") == false || _isExplosion == true)
{
shakeAmount = _amnt;
InvokeRepeating ("BeginShake", .01f, .02f);
Invoke ("StopShake", _leng);
}
}
// Update is called once per frame
void BeginShake ()
{
if (shakeAmount > 0)
{
Vector3 myPos = transform.position;
if (Vector3.Distance(myPos, transform.position) <= shakeAmount/30)
{myPos = transform.position + Random.insideUnitSphere * shakeAmount;}
transform.position = Vector3.Lerp(transform.position, myPos , 1f);
}
}
void StopShake ()
{
CancelInvoke ("BeginShake");
//enemyAIClone.stun = false;
}
Update: I tested to make sure StopShake() is being invoked everytime. It is.
Also tested to see if BeginShake() was ceasing to invoke, also good.
I'm completely stumped. Does anyone know a little more about A* to explain this?
It is using a node system for waypoints and then RB.AddForce(Direction, F$$anonymous$$ode) for movement.
Your answer
Follow this Question
Related Questions
Camera registering it is at correct position when it isn't 0 Answers
LocalPosition change affecting actual Position? 1 Answer
gameobject position lerp 0 Answers
Lerp Confusion 1 Answer
Creating points for robot (End Effector) to go through them 0 Answers