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
1
Question by junkrar · Mar 22, 2013 at 04:02 AM · movement2d-platformerenemy

Enemy Movement and Pacing

I'm creating a 2D platformer and I'm working on getting some small enemies wandering around that will hurt the player. My plan for now is to just have them pace back and forth. Their script is as follows:

 public class MinionScript : MonoBehaviour
 {
     
     float speed = 20.0f;
     float origX;
 
     // Use this for initialization
     void Start ()
     {
         //Vector3 origPosition = transform.position;
         origX = transform.position.x;
     }
     
     // Update is called once per frame
     void Update ()
     {
         transform.Translate(speed*Time.deltaTime,0,0);
         
         if(Mathf.Abs(origX - transform.position.x) > 5.0f)
         {
             speed *= -1.0f; //change direction
         }
     }
 }

I'm having trouble with the movement (the only thing defined in Update()). Sometimes it will pace, other times it will stutter and vibrate in one place, as if it's quickly changing directions. Why might this be? Is there a better way to get this enemy to pace?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Chronos-L · Mar 22, 2013 at 07:19 AM

other times it will stutter and vibrate in one place

Let's say your enemy move to right when the game start, when the enemy reached the end of their march, transform.position.x will be larger than 5.0f (let's say 5.15f), then you flipped the speed (speed now becomes -20f).

In the next Update(), there are chances that that the Time.deltaTime is just small enough, so that your transfrom.position.x become 5.05f instead of smaller than 5f (4.9f something). Then you flipped again, speed now becomes 20f.

In the next Update(), your enemy will move slightly to the right (`transform.position.x` = 5.20f ), and the speed is flipped again. This will become a vicious cycle that makes the enemy stuck on same spot. (The position.x is going back and forth in such a way: 5.2, 5.05, 5,23, 5.07, 5.3, 5.1, 5.2, 5.05 .... )

Correction on your logic:

 private float useSpeed;
 
 ...
 
 if( origX - transform.position.x > 5.0f)
 {
    useSpeed = -speed;
 }
 else if ( origX - transform.position.x < -5.0f ) {
    useSpeed = speed;
 }
 
 transform.Translate(useSpeed*Time.deltaTime,0,0);
 
Comment
Add comment · Show 2 · Share
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 junkrar · Mar 22, 2013 at 01:42 PM 0
Share

Your explanation of my issue makes sense. However, if I don't use absolute value on the subtraction (as your answer suggests) the enemy just keeps going and going in one direction. If I add the absolute value back in, then it stutters in one place like it did before. EDIT: I just noticed you made it negative, which removes the need for absolute value. So when I put the exact code you have, the enemy now just stands still.

EDIT: I fixed my code, just had to switch the values around a bit in the conditionals:

     void Update ()
     {
         if(moving)
         {
             if(origX - transform.position.x > distance)
             {
                 useSpeed = directionSpeed; //flip direction
             }
             else if(origX - transform.position.x < -distance)
             {
                 useSpeed = -directionSpeed; //flip direction
             }
             //Debug.Log (origX + " - " + transform.position.x + " = " + (origX - transform.position.x));
             transform.Translate(useSpeed*Time.deltaTime,0,0);
         }
     }
     
avatar image Chronos-L · Mar 22, 2013 at 11:54 PM 0
Share

I didn't nail the condition and direction combination correctly. I am glad that you got it right.

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

11 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

Related Questions

How to make an enemy pace? 1 Answer

How can I set my cube moving left and right with custom switch? 1 Answer

Vector3 MoveTowards character flying up when told to follow,Vector3 MoveTowards object flying up when I press play 0 Answers

2D Pathfinding Top Down 0 Answers

c# array of game objects movement help 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