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
1
Question by AKindChap · Jan 11, 2016 at 07:57 AM · movementmovement scriptmovetranslatemove an object

Slowly move a GameObject on 1 axis, then destroy it.

Hi,

I want to make it so if a gameobject's x position is positive, I want it to slowly move until it's x position is 3, and then destroy itself (and the same for going right if the x position is negative), but no matter how much I try to understand what everyone has said it just moves instantly.

Here's what I have

 //If the object is on the left, move left.
             if (scoreValue.transform.position.x > 0)
             {
                 while (scoreValue.transform.position.x < 3)
                 {
                     scoreValue.transform.Translate(Vector3.right * 1f, Space.World);
                 }
                 Destroy(scoreValue)
             }
             else
             {
                 while (scoreValue.transform.position.x > -3)
                 {
                     scoreValue.transform.Translate(Vector3.left * 1f, Space.World);
                 }
                 Destroy(scoreValue)
             }

Thanks!

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
2
Best Answer

Answer by JedBeryll · Jan 11, 2016 at 08:20 AM

It's inside the update function right? Don't use the "while" as that will be completely executed in 1 frame. Use time.deltaTime to move smoothly like this:

      if(scoreValue.transform.position.x < 3)
      {
          scoreValue.transform.Translate(Vector3.right * time.deltaTime * speed, Space.World);
      }

"speed" is a float. if you want slow try setting it to 1-3, or experiment with values.

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 LazyElephant · Jan 11, 2016 at 08:50 AM 1
Share

Just to expand on this, the Update() function is called once per iteration of the main game loop and any changes made are rendered to the screen later in the loop. This means that, in order to see the movement on screen, it has to take place over multiple calls to Update().

By putting your movement code into a while loop, you've created a nested loop. The while loop will complete every time the Update() method runs, so your objects will always appear to move immediately every frame.

avatar image AKindChap · Jan 11, 2016 at 01:37 PM 0
Share

Sorry, I forgot to say. It's inside a void. Very new to this... first real try at making something.

I'll see if what you said still works. Thanks a lot.


Okay it's still doing the same thing! I'm guessing it's because it's in a void ins$$anonymous$$d of update? I'm really stumped with this one.


Okay, apparently Translate inside a void does nothing? $$anonymous$$aybe I'm just doing it wrong (quite likely). If I remove the Destroy part the object just sits there. It wasn't moving quickly, it was just killing itself.

I moved it into update and took away Destroy and now it works perfectly. Thank you!

 //Destroy the value that was chosen
 float speed = 3f;
         //If the object is on the left, move left.
         if (scoreValue != null && scoreValue.transform.position.x > 0)
         {
             if(scoreValue.transform.position.x < 3)
             {
                 scoreValue.transform.Translate(Vector3.right * Time.deltaTime * speed, Space.World);
             }
             else
             {
                 Destroy(scoreValue);
             }
         }
         else if (scoreValue != null && scoreValue.transform.position.x < 0)
         {
             if(scoreValue.transform.position.x > -3)
             {
                 scoreValue.transform.Translate(Vector3.left * Time.deltaTime * speed, Space.World);
             }
             else
             {
                 Destroy(scoreValue);
             }
         }
avatar image KdRWaylander AKindChap · Jan 11, 2016 at 01:56 PM 1
Share

If the void is in the Update, that's indeed the same thing ! If you're holding chocolate in your hand it doesn't matter that there's paper around it, you're holding the chocolate ^^

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

move enemy to old position of player 2 Answers

How can I make a prefab object from a list to move?,How can I make an object from a list to move by itself? 2 Answers

translate 2 Answers

Character rotates left and right instead of going forward. 1 Answer

How can i make diagonal movement? 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