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 _Blvssxd_ · Sep 08, 2016 at 06:23 AM · movementtransform.positionienumeratormove an objecttransform.translate

How to move an object smoothly while in a Coroutine / Ienumerator?

I know this question has been asked before, and I've tried almost everything. My object is traveling around a cylinder and I would like for it to move over a certain amount of space on the press of an arrow key. The problem I'm having is that it just jumps over to the other position, when I would like for the change to be gradual and for the object to move more smoothly. I'm using transform. translate, but I've tried all the other solutions I've found on the internet ( transform. position = etc.)
Help is greatly appreciated, thanks. (Also, I'm not sure if it says but I'm using c#.)

 if (Input.GetKeyDown (KeyCode.RightArrow))
             StartCoroutine (switchright ());
         if (Input.GetKeyDown (KeyCode.LeftArrow))
             StartCoroutine (switchleft ());
     }
         IEnumerator switchright () {
             yield return new WaitForSeconds (1);
         transform.Translate (Vector3.right * brem);
 
                 }
             IEnumerator switchleft () {
         yield return new WaitForSeconds (1);
         transform.Translate (Vector3.right * berm );  
         }
 
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 doublemax · Sep 08, 2016 at 07:02 AM 0
Share

http://answers.unity3d.com/comments/1235406/view.html

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by teh1archon · Sep 08, 2016 at 12:46 PM

Dude, you've forgotten to factor in time. If "brem" is your speed then it should be: transform.Translate (Vector3.right berm Time.deltaTime);

Using a coroutine is no different in that matter than using it in Update.

Also, above code will still not work because in CoRo you need to make your own loop for this to move continuously. And you probably want to slerp the movement like here: https://docs.unity3d.com/ScriptReference/Vector3.Slerp.html

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 _Blvssxd_ · Sep 08, 2016 at 03:47 PM 0
Share

Thanks man , but when I use time.Deltatime its still choppy and it barely changes position. I also want to move it a fixed distance, not continuously , but I'll look into Vector3.slerp. Thanks

avatar image
1

Answer by AurimasBlazulionis · Sep 08, 2016 at 08:50 AM

I would suggest not using coroutines, instead use Vector3.Lerp inside update loop. When you press a button, store the position of the object and Time.time in variables. Then, you also have time for the stuff to happen, for example 2.0, you can also store the value of it somewhere like float moveTime so it can be changed. Then target position is how much you want to move the object. So the lerp function should look like this:

Vector3.Lerp(transform.position, transform.position + targetPosition, 1f / moveTime * time.deltaTime);

Just assign position of the object to it and you should be good to go.

You will need to do an if statement first, if you should call the Lerp function. it looks like this:

if (Time.time - startTime < moveTime)

If you use this in FixedUpdate, replace Time.time with Time.fixedTime and Time.deltaTime with Time.fixedDeltaTime.

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 _Blvssxd_ · Sep 08, 2016 at 03:52 PM 0
Share

Thanks, but as I mentioned before, it is rotating around a cylinder. Therefore the position in y and z are constantly changing, I've tried storing the y and z in variables that change in update, but it was always a little behind causing the object to fall out of the camera's view.

avatar image AurimasBlazulionis _Blvssxd_ · Sep 08, 2016 at 05:56 PM 0
Share

Oh right, so change startPosition with transform.position and targetPosition with transform.position + whateverAmountYouWantTo$$anonymous$$ove inside the Lerp function.

avatar image AurimasBlazulionis _Blvssxd_ · Sep 08, 2016 at 06:00 PM 0
Share

Not done yet, you will need to change the time thing. I believe an if statement like this: if((Time.time - startTime) / moveTime < 1f) would work, execute the interpolation function if that statement passes, and the time modifier would be 1f / moveTime * Time.deltaTime or 1f / moveTime * Time.fixedDeltaTimeif you want to do this in FixedUpdate.

avatar image AurimasBlazulionis _Blvssxd_ · Sep 08, 2016 at 06:08 PM 0
Share

I edited the answer so no one will have to look in the comments.

avatar image
0

Answer by aditya · Sep 08, 2016 at 06:34 AM

Give Vector3.MoveTowards a try, use it in Update method and you are good to go ... Good Luck

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 _Blvssxd_ · Sep 08, 2016 at 03:42 PM 0
Share

Thanks, but I can't use it in update because I need to be delayed after the key press so I need it in the coroutine. Also, I have already tried movetowards but it didn't fix my problem.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GameObject won't move with transform.translate or rigidbody2d.moveposition,GameObject won't move in void Start() 1 Answer

Moving an object on different axis? 1 Answer

Character input key doesnt work 1 Answer

noobNeedsHelp with writing script for movement 0 Answers

Character Transform shifting after colliding with objects 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