Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
11 Jun 22 - 14 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
2
Question by jakejolli · Aug 11, 2015 at 12:35 AM · c#movementtransformvector3time

Move from one position to another in x seconds

This should be simple, I know, but for some reason, I can't seem to wrap my mind around it...

I want to move an object from one position to another in X number of seconds. I can't figure out how to make it work correctly.

I would like to use Vector3.MoveTowards(), but will take any solution.

I tried taking a mathematic approach, but I haven't touched much math since high school, so be nice.

The way I've always used MoveTowards() is with the last argument essentially equating to the speed at which the transition should happen. I'm not sure if this is correct or not, as the last argument listed as "Max Distance Delta" doesn't seem all that intuitive.

Anyway... I figured I needed to calculate the speed for my final argument, so I deduced:

distance = d

speed = s

time = t

d = s * t

so

s = d / t

I calculate the distance that needs to be traveled by subtracting my two distances (although they're vector3s only the y value is different), let's call this value posDiff.

Then, I attempt my transition as follows:

 transform.position = Vector3.MoveTowards(transform.position, newPosition, Time.DeltaTime * (posDiff / transitionDuration))


I use an intermediate value for the transform, then assign that value to my gameobject's transform in my actual code, but for demonstration purposes, I think the above suffices.

Any help would be awesome.

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

2 Replies

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

Answer by allenallenallen · Aug 11, 2015 at 12:38 AM

http://answers.unity3d.com/questions/296347/move-transform-to-target-in-x-seconds.html

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
avatar image
1

Answer by toddisarockstar · Dec 30, 2018 at 02:18 PM

if you create a timer and figure the percentage of the timer that has passed. you can use that number to multiply to the difference of the two points.

here is the most basic math for this in case anyone is not understanding:

     public float seconds = 10;
     public float timer;
     public Vector3 Point;
     public Vector3 Difference;
     public Vector3 start;
     public float percent ;
     void Start(){
         start = transform.position;
         Point = new Vector3 (5, 5, 5);
         Difference = Point-start;
 }
 
     void Update(){
 
         if (timer <= seconds) {
             // basic timer
             timer += Time.deltaTime;
             // percent is a 0-1 float showing the percentage of time that has passed on our timer!
              percent = timer / seconds;
             // multiply the percentage to the difference of our two positions
             // and add to the start
             transform.position = start + Difference * percent; 
                }}

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

28 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

Related Questions

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

fixed distance for Vector3.moveTowards ? 1 Answer

Multiple Cars not working 1 Answer

How do I Round with Vector3.up 3 Answers

Guided missile help c# 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