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 Danao · Oct 06, 2014 at 10:56 AM · movementcollidertransformspeedfloat

How to move an object back/forth on a set path?

Hello, I am moving an enemy object with a collider 2D and rigidbody2D back and forth on top of a box collider 2D platform. The enemy moves back and forth without issue sometimes. Other times, the enemy seems to get stuck and just vibrate back and forth. Here is the code I am using. Any suggestions would be awesome!:

     public float speed;
     public float distance;
     private float xStartPosition;
 
        void Start () {
                 xStartPosition = transform.position.x;
                         }
        void Update () {
         
         if (transform.position.x < xStartPosition || transform.position.x > xStartPosition + distance)
         {
             speed *= -1;
             }
         transform.position = new Vector2(transform.position.x + speed * Time.deltaTime, transform.position.y);
     }
 }

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

2 Replies

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

Answer by Bunny83 · Oct 06, 2014 at 01:25 PM

Well, the reason is quite simple. Imagine Time.deltaTime is quite large when you read on end of your path. That means you move a certain amount over your turning point. The next frame if detects that and reverts the direction. Now if the Time.deltaTime is lower this time you might not move far enough to get back into the valid area so you're still outside your turning mark. That's why you change direction every frame.

You have to do the check selective for each side:

 public float speed;
 public float distance;
 private float xStartPosition;
  
 void Start () {
     xStartPosition = transform.position.x;
 }
 void Update () {
     if ((speed < 0 && transform.position.x < xStartPosition) || (speed > 0 && transform.position.x > xStartPosition + distance))
     {
         speed *= -1;
     }
     transform.position = new Vector2(transform.position.x + speed * Time.deltaTime, transform.position.y);
 }

Thist should work as you only switch direction when you actually move in the wrong direction

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 Danao · Oct 06, 2014 at 02:06 PM 0
Share

Thanks Bunny83. This seems to have taken care of the issue! I've marked the answer as correct and I'll give it another play-through to be sure :)

avatar image GabrielTheSnake · Jan 01, 2021 at 02:45 AM 0
Share

I tried this script and it works fine but it always start moving to the right. i did some $$anonymous$$or changes to the script so the platform starts moving when i step on it and i want it to go to the left first. I tried making the distance float negative or rotating the platform but it doesn't work. any idea how to fix it?

avatar image
1

Answer by Seneral · Oct 06, 2014 at 12:46 PM

This is quite simple, Imagine your character outside of this boundaries: Every frame the direction would be swirched, write something like this:

void Update () { int dir = 1; if (transform.position.x < xStartPosition || transform.position.x > xStartPosition + distance) { dir *= -1; } transform.position = new Vector2(transform.position.x + (speed*dir) * Time.deltaTime, transform.position.y); }

Note this is pseudocode and also not very clean. I wouldn't set the transform but move it. I'll let you do it;)

Comment
Add comment · Show 3 · 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 Danao · Oct 06, 2014 at 01:11 PM 0
Share

Thanks Seneral, but the direction change isn't really the problem (that works with the code above). The issue is that sometimes the movement just stops and I can't quite pin down why.

avatar image Seneral · Oct 07, 2014 at 09:20 AM 0
Share

That's what I meant to describe, switching the direction every frame results in getting stuck and "vibrating". Bunny83 explained it way better:)

avatar image Danao · Oct 07, 2014 at 12:59 PM 0
Share

Still appreciate the help! Thanks again :)

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

31 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 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

Weird shaky player behavior (moving through colliders) 0 Answers

can not move my enemy with rigidbody 1 Answer

Moving up and down (in a slope) without change to velocity or "bumping" 0 Answers

How to Move Player in Particular Direction Without holding Down a Button? 0 Answers

Jerky motion using moveTowards at high speed 0 Answers


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