Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CG-DJ · Oct 26, 2013 at 03:40 AM · positionaienemypathfindingjitter

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 )

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · Oct 26, 2013 at 02:00 PM 1
Share

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.

avatar image CG-DJ · Nov 01, 2013 at 03:18 AM 0
Share

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!

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges