• 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 binary_biscuit · Jun 19, 2014 at 02:28 PM · lerp

Lerp between two Vectors

I'm testing some 3D transformations, and while I can change transform.position to instantly change location of a game object I can't seem to get the slower Lerp method to work.

 using UnityEngine;
 using System.Collections;

 public class MoveTest : MonoBehaviour {

     Vector3 start;
     Vector3 des;

     void Start () {
         start = new Vector3(transform.position.x, transform.position.y, transform.position.z);
         des = new Vector3(transform.position.x, 10.0F, transform.position.z);
     
 } 


 void Update(){
     transform.position = Vector3.Lerp(start, des, 1.10F * Time.deltaTime);
 }


I'm a little embarrassed for asking such a simply question.

Comment
Add comment · Show 2
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 HarshadK · Jun 19, 2014 at 02:35 PM 0
Share

We would need something more than 'I can't seem to get the slower Lerp method to work'.

What is the issue that you are facing with your lerp method?

avatar image binary_biscuit · Jun 19, 2014 at 02:59 PM 0
Share

basically the game objects don't move at all. I was expecting them to slowly move to the destination.

2 Replies

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

Answer by MikeNewall · Jun 19, 2014 at 03:01 PM

The lerp method gives you a point that is a fraction of the way along a line between the start and end point. The fraction is supplied as the third parameter. If the fraction is 0 then the position is at the start of the line between the twho points, and a fraction of 1 would be at the end of the line. In your case it's always the same so the position never changes. You need to increase the fraction over time.

     public float speed = .5f;
 
     private Vector3 start;
     private Vector3 des;
     private float fraction = 0; 
 
 
     void Start () {
         start = new Vector3(transform.position.x, transform.position.y, transform.position.z);
         des = new Vector3(transform.position.x, 10f, transform.position.z);
         
     } 
     
     void Update(){
 
         if (fraction < 1) {
             fraction += Time.deltaTime * speed;
             transform.position = Vector3.Lerp(start, des, fraction);
         }
     }
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 binary_biscuit · Jun 19, 2014 at 04:30 PM 0
Share

Thank you. The answer and the explanation was what I was looking for.

avatar image
5

Answer by robertbu · Jun 19, 2014 at 03:23 PM

@MikeNewall's solution is the traditional way to use Lerp(). Here is a common non-traditional use of Lerp() that produces an eased movement:

 Vector3 des;
 
 void Start () {
     des = new Vector3(transform.position.x, 10.0F, transform.position.z);
 } 
 
 void Update() {
     transform.position = Vector3.Lerp(transform.position, des, speed * Time.deltaTime);
 }

And here is a movement by speed that does not use a timer:

 Vector3 des;
 
 void Start () {
     des = new Vector3(transform.position.x, 10.0F, transform.position.z);
 } 
 
 void Update() {
     transform.position = Vector3.MoveTowards(transform.position, des, speed * Time.deltaTime);
 }
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Problem about Rotating the Character 1 Answer

Turning character slowly with Slerp 1 Answer

Lerping the Camera closes Nexus 5, not Nexus 10 0 Answers

How do I smooth the movement of three transforms moving at different rates along a single axis? 2 Answers

Lerp Color doesn't work 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