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 braderz · Nov 19, 2014 at 05:22 PM · movement scripttransform.positionobject movementvector3.up

How to move an object to a height

Hello,

I'm new to scripting and I need to make a object slowly move up and stop at 30 on the y axis. It must be done in C#

So far I have this:

 while (transform.position.y < 30)
     {
         transform.Translate (Vector3.up * Time.deltaTime, Space.World);
     }

But the object just starts in the air. Please help.

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 ♦ · Nov 19, 2014 at 12:26 PM 0
Share

It appears that way. The thing is actually moving bit by bit but because its in a while loop you don't see the results updating, you only see the end result. Do it a bit per frame ins$$anonymous$$d to observe a movement ins$$anonymous$$d of a transform.Teleport.

avatar image braderz · Nov 19, 2014 at 12:56 PM 0
Share

That makes sense, how would I do that? What confuses me is without the loop it works fine, and slowly starts going up; but with the loop it starts in the air.

1 Reply

· Add your reply
  • Sort: 
avatar image
4

Answer by richyrich · Nov 19, 2014 at 05:56 PM

That makes sense, how would I do that? What confuses me is without the loop it works fine, and slowly starts going up; but with the loop it starts in the air.

Time.deltaTime is the time in seconds it took to complete the last frame (http://docs.unity3d.com/ScriptReference/Time-deltaTime.html).

The while loop keeps on using the same value over and over until the height is reached and only then allows other code to run that updates the graphics and allows you to see anything.

Let's suppose you ask me to use a pulley to lift a block. I can go to the pulley, start pulling and while the block is not high enough, I'll keep pulling. Once the block is at the right height, I report back and tell you the job is finished, I execute the update once - you have only seen the stone in two states - bottom (before you asked) and top.

Now let's assume I have no confidence in myself and am desperately afraid I might take the block too high. In this case I lift the block, tie it off and ask if it's high enough - I update you. You tell me no, it's not enough. So I return and pull a bit more. In this way, you see things slowly changing. That's the difference. In graphics, you don't see the change in the while because during the loop, there are no updates of the graphics, there is no pause.

In answer to your question, just replace the while with an if

 if (transform.position.y < 30f)
 {
     transform.Translate (Vector3.up * Time.deltaTime, Space.World);
 }




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 bubzy · Nov 19, 2014 at 06:14 PM 0
Share

nice explanation, thumbs up

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

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

Question on how to add a "dash" ability to sidescroller 0 Answers

How to import the object from server to unity 2 Answers

Need help with making a character jump. 1 Answer

Moving an object object x pos + 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