Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 xHowlW · Oct 01, 2018 at 05:54 PM · movementgame object

Moving an object left and right

Hi all, I'm trying to get an object to move left to a certain point and then once it reaches that point it moves right until a certain point.

I've got the object to move left, but can't get it to move right as it seems to freeze once it's reached the position I want.

void Update () {

     transform.Translate(Vector3.left * speed * Time.deltaTime);

     if (transform.position.x <= -4)
     {
         transform.Translate(Vector3.right * speed * Time.deltaTime);
         Debug.Log("Working!");
     }
    

The object goes left, and reaches to -4 on the x Axis but then freezes and doesn't move, any ideas?

Thanks!

Comment
Add comment · Show 1
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 hexagonius · Oct 01, 2018 at 06:48 PM 0
Share

that's because your first translate had never ignored. furthermore, it makes more sense you declare one speed, starting negative and once the left end condition is met you change the speed to positive until the opposite condition is met.

2 Replies

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

Answer by LCStark · Oct 01, 2018 at 06:08 PM

Think how this method works, step by step:
1. move the object left
2. check if reached the target on the left
3. if yes, then move the object right

So, when your object reaches the target spot on the left, you're moving it both left and right at the same time, in result not moving it at all.

Add a flag to check whether you're still moving left or right, then based on its value move in the direction you want:

private bool movingLeft;
void Start() {
    movingLeft = true;
}
void Update() {
    if (movingLeft == true) {
        // move left
        if (transform.position.x <= -4) movingLeft = false;
    } else {
        // move right
    }
}
Comment
Add comment · Show 4 · 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 Zarenityx · Oct 01, 2018 at 06:16 PM 0
Share

Looks like we gave the same solution at the same time lol. The performance addict in me makes me point out that using bools for this and an extra if is rather wasteful. A float comparison is not so slow as to need to only occur conditionally, and in the meantime you might get mispredictions and lose a few precious nanoseconds.

Obviously this isn't super important or anything, you would never really see that slowdown, I'm mostly just have a hyperactive olfactory sense when it comes for sniffing out micro-optimizations.

avatar image LCStark Zarenityx · Oct 01, 2018 at 06:23 PM 0
Share

Yeah, nice ti$$anonymous$$g! :) I wrote it this way to not complicate the code more than needed to get a basic solution to the problem. I'm guessing it will be rewritten multiple times to fit the changing needs of the game anyway.

avatar image xHowlW · Oct 01, 2018 at 06:16 PM 0
Share

Thank you, that worked! :)

avatar image LCStark xHowlW · Oct 01, 2018 at 06:23 PM 0
Share

You're welcome!

avatar image
1

Answer by Zarenityx · Oct 01, 2018 at 06:11 PM

The issue here is that, when you do reach -4, you nudge it to the right and then you're no longer

 //Up at the top with your variables:
 private Vector3 dir = Vector3.left;
 
 //Your Update function
 void Update(){
      transform.Translate(dir*speed*Time.deltaTime);
 
      if(transform.position.x <= -4){
           dir = Vector3.right;
      }else if(transform.position.x >= 4){
           dir = Vector3.left;
      }
 }

The important things to note here are:

  • The direction persists between frames. It only changes in very specific cases

  • It's generally a good idea to only do 1 translate call per frame and store your direction in a variable anyway. Explicitly setting the direction and speed is much more predictable and you always know why something moves the way it does.

  • I'm assuming you wanted it going from -4 to 4, although really you should store these in variables. Firstly, you can see these in the inspector, but secondly, the numbers -4 and 4 on their own don't mean anything on their own. On the other hand, 'leftExtent' and 'rightExtent' might be -4 and 4, but are much more meaningful and, if you use these extents for other things, you don't have to change these magic numbers everywhere.

Comment
Add comment · Show 1 · 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 xHowlW · Oct 01, 2018 at 06:18 PM 0
Share

Thank you! And thanks for explaining it too :)

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

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

Move the enemy object opposite to player and keep its distance relative to the player? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Making a bubble level (not a game but work tool) 1 Answer

Player moves slowly by itself? 0 Answers

Change int value for 30 sec... 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