Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
0
Question by Borzi · Dec 02, 2013 at 11:52 AM · vector3transform.positionvector3.movetowards

Get an Object to move to a Position for a Fixed time

Hey, I am trying to get this object to move to "EndPoint". The code is working, I just don't want to set the speed it moves at, but the time it takes to reach its destination. Anyone got any ideas of how to fix this? Thank you in advance!

  void Update () 
     {
     if(EndPoint != Vector3.zero)
         {
         myTransform.position = Vector3.MoveTowards(myTransform.position, EndPoint, Time.deltaTime * MuzzleVelocity);
         }
     if(myTransform.position == EndPoint)
         {
         Destroy(gameObject);
         Debug.Log ("Destroyed Object");
         }
 }
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
2
Best Answer

Answer by yatagarasu · Dec 02, 2013 at 03:14 PM

Time and speed are easily interchangeable. You can take the distance to the destination divide it by time you want your object to move and you'll get the speed your object should move with.

Comment
Add comment · Show 2 · 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 Borzi · Dec 02, 2013 at 04:24 PM 0
Share

I dont know why I didn't think of this before -.- thanks!

avatar image sandorlajos · Mar 10 at 05:09 PM 0
Share

Luv u with all of my hearth

avatar image
0

Answer by MrVerdoux · Dec 02, 2013 at 12:01 PM

I use enumerators for this:

 private float movementTime = 2;
 private float lerpTime = 0.05f;
 
 void Start(){
     StartRoutine("MoveTo", new Vector3(1,0,0);
 }
 
 IEnumerator MoveTo(Vector3 destination){
     float timeEllapsed = 0;
     Vector3 origin = transform.position;    
 
     while (timeEllapsed < movementTime){
         transform.position = Vector3.Lerp(origin,destination,timeEllapsed/movementTime)
         timeEllapsed += lerpTime;
         yield return new WaitForSeconds(lerpTime);
     }
 }

Haven´t tested particularlly this one, it might need you to fix it somewhere!

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 Borzi · Dec 02, 2013 at 01:45 PM 0
Share

What is ellapsedTime?

avatar image MrVerdoux · Dec 02, 2013 at 02:53 PM 0
Share

it is the amount of time that has passed since the coroutine began. It´s a local variable because you don´t have to use it anywhere, you just set the total amount of time.

Notice that in the begining ellapsedTime = 0 and every loop it will be a little bigger. In the end, the condition to get out of the loop (and then finish the coroutine) is that the ellapsed time is bigger than the total time of the movement.

And how do you get to move from any point to any other in a certain amount of time? Well, I used a linear interpolation. That is (Notice that linear interpolation is normally named "Lerp"):

 float LinearInterpolation(float from, float to, float t){
    return from*(1-t) + to*t;
 }

I used the same thing for a Vector3. If you want a soft movement you might want to use a different kind of interpolation.

avatar image Borzi · Dec 02, 2013 at 03:44 PM 0
Share

You haven't defined the variable ellapsedTime in the code though and I don't know what I have to put...do you mean timeEllapsed? In this case there is an error in the code.

avatar image MrVerdoux · Dec 02, 2013 at 03:57 PM 0
Share

You are right! There was an error.

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

19 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

Related Questions

Why is my y-axis bugging? 1 Answer

Trouble converting transform.position to C# 1 Answer

How to maintain player chosen camera height? 0 Answers

Vector3.Lerp making my object invisible. 2 Answers

Object keeps moving when using Vector3.Lerp on transform.position 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