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 /
  • Help Room /
avatar image
0
Question by Rebel8909 · Feb 06, 2016 at 07:10 PM · javascriptlerp

Objects not moving smoothly with Vector3.Lerp

So I'm trying to make a drawer of sorts that will pull a couple of prefabs from out of the frame. However I've been trying to get them to smoothly move from out of frame into frame and I can't seem to get it right.

I've been fiddling with the Vector.Lerp() but it doesn't seem to work like I thought it would.

After detecting if the "drawer handle" has been clicked I have the objects set to move with :

 var smoothTime = 2;        
 var atomSpawner : GameObject;
 
 var startPos : Vector3;
 var endPos : Vector3;
 
 function Update(){
 if(drawerOpen == true){
     if(hit.collider.tag == "slider"){
         atomSpawner.transform.position = Vector3.Lerp(atomSpawner.transform.position, startPos, Time.deltaTime * smoothTime);    
     drawerOpen = false;
     }
 }
                     
 else{
     if(hit.collider.tag == "slider"){
         atomSpawner.transform.position = Vector3.Lerp(atomSpawner.transform.position, endPos, Time.deltaTime * smoothTime);
         drawerOpen = true;
         }
     }
 }


I've tried messing around with the smoothTime number and that doesn't seem to affect it all that much, I've also tried swapping out atomSpawner.transform.position with either endPos and startPos, which just cause it to go from endPos to startPos / startPos to endPos immedietly.

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 Teravisor · Feb 06, 2016 at 08:50 PM 0
Share

lerp should be used with start position, end position and current time in (0...1) area like: Vector3.Lerp(startPos,endPos,time); with time being 0 or lower means object is in startPos, time being 1 or higher means object is in endPos. So you should first remember start time. Then current time $$anonymous$$us start time divided by smoothTime is what you supply to Lerp.

2 Replies

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

Answer by Jessespike · Feb 06, 2016 at 08:53 PM

http://docs.unity3d.com/ScriptReference/Mathf.Lerp.html

You need to pass a value that goes from 0 to 1 and you need a start and end position. Try something like:

 float lerpTime = 0f;
 ...
 lerpTime = Mathf.Clamp(lerpTime + Time.deltaTime * smoothTime, 0f, 1f);
 atomSpawner.transform.position = Vector3.Lerp(startPos, endPos, lerpTime);    
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 NoseKills · Feb 07, 2016 at 12:17 PM 0
Share

Hmm. In my experience, even when using Lerp 'the wrong way' the movement is relatively smooth as long as deltaTime is used.

$$anonymous$$aybe i'm having trouble reading this on mobile but to me it looks like the script would make drawerOpen toggle after each Lerp call and make it alternate Lerping the object in and out in consecutive Update calls...

Well, glad if this helped the OP get the issue fixed :)

avatar image
0

Answer by Ideka · Feb 07, 2016 at 12:41 AM

Vector3.Lerp doesn't take a "velocity" or "lerp amount," it takes a percentage, i.e. a float from 0 to 1. As the docs say:

When t = 0 returns a. When t = 1 returns b. When t = 0.5 returns the point midway between a and b.

Try Vector3.MoveTowards.

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

44 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 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

Is there a way to smoothly change a float number between two numbers when a key is pressed? 1 Answer

Change of Scene 1 Answer

Multiplayer 2 Answers

How to switch mouse with arrows ! 0 Answers

render out the audio spectrum data to a UI text 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