- Home /
Enemy Moves Jitters when Supposed to Be Still
Ok, this one may be a little hard to describe, and my screencap software is acting up.
I am making a zombie platformer game. I want the zombies to gather underneath the player if he is on a platform the zombies can't reach. So I have them match their X value to the players X value. I also add a random number to that so that it randomizes a bit, so that a cluster of zombies will form. The problem is, I think I am setting this number wrong. The zombie hits the place where it is supposed to be, but then its jitters back and forth. What is happening, is it repeating "Point 1" and "Point 2" (I commented them for reference), switching back and forth between them. What should I do so that they just stop at this spot when they reach it, but then move with the player. Thanks for the help! Comment if you need more info!
//Enemy Script. I only added the parts I think you need.
function Chase()
{
LookForPlayer(); //Does the enemy see the player?
var targetPos : float = (playerObj.transform.position.x - transform.position.x);
if(!see) //If the enemy is in chase mode, but doesn't see the player, a countdown timer starts, counting down to switching modes to idle.
{
chaseTimer--;
}
if(playerLoc.y > 2) //If the player is above the zombie
{
Debug.Log("player above");
targetPos += distToStopUnder; //Adding a random number so that a horde gathers under the player
if(targetPos > 0) //*Point 1* If the place the zombie wants to get is to the right of him.
{
Debug.Log("greater than");
if(vMod > 0) //If we are going the right direction, do nothing.
moveBool = true;
else //If we are going the wrong direction, turn around.
{
vMod *= -1;
vX = 0;
}
}
else if(targetPos < 0) //*Point 2* If the place the zombie wants to get to to the left of him.
{
Debug.Log("less than");
if(vMod < 0) //If going the right way, do nothing.
{
moveBool = true;
}
else if(vMod > 0) //If going the wrong way, turn around.
{
vMod *= -1;
vX = 0;
}
}
else if(targetPos == 0) //This is a test statement, to see if I'm ever hitting 0. I'm not (yes, I am aware that this is too many else ifs :P).
{
Debug.Log("here");
}
}
else //playerLoc < 2 //The player level with the zombie.
{
Debug.Log("player level");
if(targetPos > 0) //This is all exactly the same as above, and the problem happens on the ground too!
{
if(vMod > 0)
{
moveBool = true;
}
else if(vMod < 0)
{
vMod *= -1;
vX = 0;
}
}
else if(targetPos < 0)
{
if(vMod < 0)
moveBool = true;
else if(vMod > 0)
{
vMod *= -1;
vX = 0;
}
}
}
}
function LookForPlayer() //Remember, the enemies rotation does not change, only the transform direction.
{
see = false;
if((transform.InverseTransformPoint(playerObj.transform.position).x * vMod) > 0) //Check if the player is in front or behind enemy. Enemy direction is based on vMod (+ = right, - = left).
{
if(Mathf.Abs(playerObj.transform.position.x - transform.position.x) < awareDistStand)
{
if(playerObj.transform.position.y - transform.position.y < awareHeight)
{
var hitInfo : RaycastHit;
Debug.DrawRay(eyes.transform.position, transform.right * vMod * awareDistStand, Color.red, .1);
if(Physics.Raycast(eyes.transform.position, transform.right * vMod, hitInfo, awareDistStand, layerMask)) //Multiply raycast direction by vMod, because vMod dictates direction.
{
if(hitInfo.collider.tag == "Player")
{
see = true;
}
}
}
}
}
if(state == AIState.Chase)
{
playerLoc.y = (playerObj.transform.position.y - transform.position.y); //I change this from the original. it was x. This is setting up how high the player is
}
}
function FixedUpdate() //----------------Fixed Update
{
if(moveBool) //If we want the enemy to move, than moveBool is true.
{
if(vX < maxSpeed) //Setting up what velocityX should be.
vX += speedUp;
else if(vX > maxSpeed)
vX = maxSpeed;
rigidbody.AddForce(Vector3.right * vX * vMod * Time.deltaTime, ForceMode.VelocityChange); //Move velocity X
moveBool = false; //Reset moveBool.
}
}
(sorry, I know its a ton of code. Here is a link to the rest of the script if you need it: https://dl.dropboxusercontent.com/u/33872519/Pushing%20Enemies/EnemyScript.js )
Specify a margin of error for your positioning. Getting floats to settle at exact values is a nightmare.
It possible your model is very slightly overshooting which will keep it moving back and forth.
Sorry for taking so long to respond. Life == hectic.
Yep that was it! I should have known better. That's what happens when you stop coding for a month. Thanks (again) for the help meat!
Your answer
Follow this Question
Related Questions
Enemy units behave normally, until they rotate, then their AI seems to break 0 Answers
Obstacle avoidance not working 0 Answers
Horde of NavMeshAgents - stops to recalculate path. 4 Answers
How to I make FindGameObjectWithTag() not just find itself? 1 Answer
sprite changes z position without any input to do so 1 Answer