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 gamerant · Nov 09, 2013 at 08:23 PM · javascriptmovementscipting

How to make a gameObject move from it's current position in a different direction after a set amount of time

EDITED:

Hello everyone.

I have a sphere that I am moving using rigidbody.velocity. I would like to after a set time in this case 5 seconds tell the sphere to move in a new direction. For example the sphere starts moving to the left,after 5 seconds are up the sphere starts moving upwards. So after every 5 seconds that pass the sphere keeps changing it's movement direction.

In order to achieve this I was thinking that I could keep track of the sphere's position throughout its travel time.Then when the seconds are up I assign the position it is at as the new position.Another words I want to have the sphere take it's current position as the new starting point from which it moves off.

I tried to do this by tracking the position of the sphere at run time and the after 5 seconds are up assigning it to a new variable.But that has gotten me nowhere.

This is my timer code:

 function LateUpdate () 
 {
     print("late update");
     
     ParticleMovement();///Function that controls sphere movement    
     
     if(movementTimer > 0)
     {
         movementTimer -= Time.deltaTime;
         print(movementTimer);
     }
     
     if(movementTimer <= 0)
     {
         ParticleMovement();//I'm calling the movement function here when timer
                                //reaches zero so that the particles direction
                                //is chosen again.
         movementTimer = newTimer;//Reset the timer back to 5 seconds every time
     }
 }

All hints are welcome :D

Thank you.

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 Huacanacha · Nov 09, 2013 at 08:44 PM 0
Share

For one don't compare Vectors directly for equality, for the same reason you don't compare floats for equality. Try Vector3.Distance(newPosition, CurrentPosition) < 0.01 or something.

Secondly why do you need this check at all? Don't you want to change direction after 5 seconds regardless, in which case this test is pointless?

You mention a few times ' set the current position of the sphere as it's origin point '... this doesn't make sense to me as the position of a primitive sphere will always be it's origin point.

$$anonymous$$aybe post some more of your code for context and try explaining exactly what you want again, just so we can be perfectly clear.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by delpontej · Nov 09, 2013 at 08:43 PM

Your newPosition and currentPosition values probably will never be exactly the same value, which is what is being tested in your example. Especially, if the movement is performed overtime. In stead of checking if newPosition is currentPosition, I'd suggest calculating the distance between the two points, and if the returned distance is less than an acceptable margin the currentPosition is 'close enough' to the new position. From there if you really want you could snap the currentPosition to your newPosition if needed.

Comment
Add comment · 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

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

18 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

Related Questions

Slowing Down When Sliding Across Walls 2 Answers

Horizontal axis not working after animation 1 Answer

I am trying to make a movement script for my main camera but it isnt working! 2 Answers

Move Object Smoothly 1 Answer

Rigidbody.MovePosition doesn't move reliably? 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