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 platypusstudio · Apr 01, 2012 at 04:48 PM · movementvector3lerpanimated

Smooth translation

I'm trying to get a gameObject to move smoothly to a certain position (defined in a Vector3 variable), and I've been told to use Vector3.Lerp. But, when I do, the object doesn't move smoothly, it just moves to the target in one frame. Here is my code:

 var start : Vector3;
 var end : Vector3;
 transform.position = Vector3.Lerp(start,end,Time.deltaTime);


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
1

Answer by Ipsquiggle · Apr 01, 2012 at 10:03 PM

The 3rd parameter of Lerp is a ratio from 0 to 1, where 0 means start, 1 means end.

So you'll want to do something like this:

 var lerpPosition : float = 0.0f;
 var lerpTime : float = 5.0f; // This is the number of seconds the Lerp will take
 var start : Vector3;
 var end : Vector3;
 function Update() {
     lerpPosition += Time.deltaTime/lerpTime;
     transform.position = Vector3.Lerp(start,end,lerpPosition);
 }

So what's happening here is that we start with a lerpPosition of 0 (putting our instance at start), and each frame we add a small amount to it. After a few seconds (5 in this example), lerpPosition will have increased to 1.0, putting our instance at end.

This way you can also easily tell that the Lerp is complete by testing if (lerpPosition >= 1.0f).

Hope that helps.

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
0

Answer by fnc12 · Jan 31, 2014 at 08:58 AM

Vector3.Lerp does not make a smooth movement. It calculates a new vector in fraction between two vectors. Imagine: you have start point with x=5 and finish point with x=10. So Lerp(5,10,0.5) returns 7.5 because the third parameter is 0.5 that is half of the way is passed; Lerp(5,10,1) returns 10 (the end of the way), Lerp(5,10,0) returns 0 and so on. In your case you have to store fraction (the third parameter), increment it every frame and assign a new transform to your object every frame until fraction equals or greater that 1. After that you get the smooth movement.

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 fnc12 · Jan 31, 2014 at 08:57 AM 0
Share

Vector3.Lerp does not make a smooth movement itself. It calculates a new vector in fraction between two vectors. For example: you have start point with x=5 and finish point with x=10. So Lerp(5,10,0.5) returns 7.5 because the third parameter is 0.5 that is half of the way is passed; Lerp(5,10,1) returns 10 (the end of the way), Lerp(5,10,0) returns 0 and so on. Lerp make the same thing in 3D. In your case you have to store fraction (the third parameter), increment it every frame and assign a new transform to your object every frame until fraction equals or greater that 1. After that you get the smooth movement.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Weapon Movement Freezing 0 Answers

Vector3.Lerp Only working once 1 Answer

Vector3 Lerp using Time.deltaTime? 3 Answers

Vector3 Lerp finish too quicly 1 Answer

How to smooth my camera (Vector3.Lerp) 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