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 iso250 · Jul 02, 2015 at 07:19 AM · lerpvectorvector2

Having difficulty with Lerp

Hello all.

I'm a relatively new user and have been banging my head against this for a while. I am trying to get an object to lerp but it only ever moves once for the value of the float at the end of the lerp statement. Here is my code. (It's for a sliding door.)

 using UnityEngine;
 using System.Collections;
 
 public class VertUpDoor : MonoBehaviour {
 
     private Vector2 InitialPosition;            //  Records door's initial position.
     private Vector2 DestinationPosition;        //  Where the door will open to.
 
 
     void Start () {
     
         InitialPosition = new Vector2 (transform.position.x, transform.position.y);    //  Records the door's original position.
         DestinationPosition = new Vector2(transform.position.x, transform.position.y + 0.9f);  //  Provides the doors destination position.
 
     }        
 
     void FixedUpdate () {
     
     }
     
     void OnTriggerStay2D (Collider2D col) {
 
         if (col.gameObject.tag == "DoorOpener")
         {
             transform.position = Vector2.Lerp(InitialPosition, DestinationPosition, 0.2f);
         }    
     }        
 }


I'm assigning values to InitialPosition and DestinationPosition as I want to make a prefab as all doors will slide by +0.9f in the Y direction. The objects moving through the sliding doors have "DoorOpener" attached as tags.

I've looked through tutorials and the forums but cannot get it to work. I'm feeling really stupid at the moment and am hoping someone can help me.

Thank you.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer Wiki

Answer by starikcetin · Jul 02, 2015 at 07:23 AM

That's because you don't update the first lerp value. Try this:

 transform.position = Vector2.Lerp(transform.position, DestinationPosition, 0.2f)
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 iso250 · Jul 02, 2015 at 11:37 AM 0
Share

I was under the (incorrect) assumption that the starting position was held in memory until the lerp had completed itself. Now my doors are working perfectly.

Thank you for your help!

avatar image
4

Answer by Hellium · Jul 02, 2015 at 07:38 AM

It seems you didn't get what is LERP in fact.

LERP means "Linear intERPolation". With this mathematic tool, you are able to interpolate a value between two others using a linear function ( y = ax + b ) and a coefficient t between 0 and 1.

Supposing you are trying to get a position (C) between two others (A and B) (like your problem), you have the folowing drawing :

alt text

With the folowing line located in the OnTriggerStay2D function :

transform.position = Vector2.Lerp(InitialPosition, DestinationPosition, 0.2f);

The positions A and B doesn't change, neither do your parameter. Thus, you have two options :

1°) Make the parameter t vary between 0 and 1 : The movement curve will be perfectly straight : the speed will be the same over time.

  // The slide movement will take SlideDuration seconds.
  public float SlideDuration = 5 ;
  private float timer ;
 
  void OnTriggerEnter2D (Collider2D col)
  { 
      if (col.gameObject.tag == "DoorOpener")
          timer = 0 ;   
  }
 
 void OnTriggerStay2D (Collider2D col) {
  
      if (col.gameObject.tag == "DoorOpener")
      {
          timer += Time.deltaTime ;
          transform.position = Vector2.Lerp(InitialPosition, DestinationPosition, timer / SlideDuration  );
      }    
  }

 

2°) Change the position of your position A : The movement will decelerate over time

 void OnTriggerStay2D (Collider2D col) {
 
      if (col.gameObject.tag == "DoorOpener")
      {
          transform.position = Vector2.Lerp(transform.position, DestinationPosition, 0.2 );
      }    
  }   

alt text

Sorry for bad drawings.


lerp.png (18.7 kB)
lerp.png (4.6 kB)
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 iso250 · Jul 02, 2015 at 11:38 AM 0
Share

Hey there. I knew about the deceleration method but not the constant method. Thank you for that!

Also, your pictures were clear and informative. Couldn't ask for more than that. :)

avatar image fafase · Jul 02, 2015 at 11:51 AM 0
Share

Use $$anonymous$$oveTowards if you want to keep the movement constant. Only the last iteration may be shorter.

avatar image
-1

Answer by Coffee-with-Venky · Jul 02, 2015 at 07:55 PM

Lerp Works only In Update !!

you can change the vector to position from another metheod !!!

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 Dave-Carlile · Jul 02, 2015 at 07:56 PM 0
Share

Lerp is just a mathematical function. It will work anywhere, doesn't care about Update. You just have to vary one or more of the parameters over time.

avatar image starikcetin · Jul 03, 2015 at 01:03 AM 0
Share

Downvote for incorrect information. Lerp can work in custom methods too.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Diagonal Movement 1 Answer

Why doesnt Vector2.Lerp work? 1 Answer

How to get click position in Vector 2 ? 1 Answer

How can I find the velocity on a rigidbody which moves using Lerp? 1 Answer

Get angle between 2 Vector2's 5 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