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 eeveelution8 · Mar 14, 2015 at 09:31 PM · javascriptlerpplatformmovingmoving platform

Moving Platform Moves instantly.

This is the specific part of the movement in the script,

 function movenow(){
 
 yield WaitForSeconds(DelayTime);
 
 transform.position = Vector3.Lerp(position1.transform.position, position2.transform.position, moveTime);
 
 yield WaitForSeconds(pausetime);
 
 transform.position = Vector3.Lerp(position2.transform.position, position1.transform.position, moveTime);
 
 hasnowmoved = false;
 ison = false;
 
 }


movetime is set to 1, so I expected the object to move from position1 to p[osition2 in one second, but does it instantly. I'm not sure if I have codedit wrong, or if it's missing something, any help would be appreciated.

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

1 Reply

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

Answer by S_Darkwell · Mar 14, 2015 at 10:01 PM

Vector3.Lerp smoothly transitions from first Vector3 to the second Vector3 as its third argument (moveTime) ranges from 0 to 1. Setting moveTime to 1 essentially tells Vector3.Lerp to return a value exactly equal to the second Vector3, which is your destination.

What you want to do instead is to begin with moveTime == 0, then add Time.deltaTime to it during Update(). This will cause it to move over the course of one second.

If you want it to take two seconds, you'd want to change the third argument to moveTime / 2f.

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 tigertrussell · Mar 14, 2015 at 10:12 PM 1
Share

Lerp is like saying "tell me the point which is X% between this start and this end." So if you do something like Vector3.Lerp(v1, v2, 0.5f) you get the halfway point; if you do Vector3.Lerp(v1, v2, 0.25f) you get a quarter of the way from v1 to v2. Therefore, like @Arismeir said, you should be updating the position during every Update() call, using Lerp to deter$$anonymous$$e what you should be updating the value to. You should also investigate $$anonymous$$athf.$$anonymous$$in() and $$anonymous$$athf.$$anonymous$$ax() so that you can ensure that your interpolation completes exactly where you intended it to.

avatar image eeveelution8 · Mar 14, 2015 at 10:19 PM 1
Share

For future use by probably other people looking for the same question, this is the script in the door,

 var ison : boolean = false;
 var direction : int = 1;
 
 var position1 : GameObject;
 var position2 : GameObject;
 
 var moveTime : float = 0;
 var journeyTime : float = 1;
 
 function Update () {
 
 if(ison == true){
 
     moveTime += Time.deltaTime;
     STOPRINE();
     if(direction == 1){
         transform.position = Vector3.Lerp(position1.transform.position, position2.transform.position, moveTime / journeyTime);
     }
     
     if(direction == 2){
         transform.position = Vector3.Lerp(position1.transform.position, position2.transform.position, moveTime / journeyTime);
     }
     
     }
 }
 
 
 
 function STOPRINE(){
 yield WaitForSeconds(journeyTime);
 ison = false;
 moveTime = 0;
 if(direction == 1){
 direction = 2;
 }else if(direction == 2){
 direction = 1;
 }
 
 }

The door will ativate when 'ison' is true, the way 'ison' will be activated depends on what the user will use thew door for.

avatar image tigertrussell · Mar 14, 2015 at 11:01 PM 0
Share

Yikes - take that code away! Put it in a Wiki or a PasteBin or something, then add a link to it ins$$anonymous$$d.

avatar image S_Darkwell · Mar 15, 2015 at 04:02 AM 0
Share

An alternative to Vector3.Lerp that can sometimes be easier to use is Vector3.$$anonymous$$oveTowards. Like Lerp, it accepts a start and an end Vector3, but ins$$anonymous$$d of the third argument accepting 0 to 1, it accepts a "step-size" float.

So, ins$$anonymous$$d of saying, "Create a Vector3 this percentage between A and B", you are saying "Create a Vector3 this distance toward A from B".

$$anonymous$$oveTowards often helps clean up my movement code.

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

21 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

Related Questions

Can't get platform to move to the other direction 2 Answers

Player on moving plattform 0 Answers

How to use Lerp? 3 Answers

How to Lerp or Slerp Time.timeScale change 1 Answer

Trying to set up a lerp position system for the camera and failing... 2 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