• 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 ledisnomad · Mar 24, 2016 at 01:45 PM · scripting problemlerplocalposition

Lerp localposition does not actually move slowly

This is what I have:

     public KeyCode k;
     private Vector3 standing = new Vector3(0.0f, 0.9f, 0.0f);
     private Vector3 sitting = new Vector3(0.0f, 0.4f, 0.0f);
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKeyDown(k))
         {
             if (transform.localPosition.y > 0.5f)
             {
                 transform.localPosition = Vector3.Lerp(standing, sitting, 3f * Time.deltaTime);
                 //transform.localPosition = sitting;
             }
             else
             {
                 transform.localPosition = Vector3.Lerp(sitting, standing, 3f * Time.deltaTime);
                 //transform.localPosition = standing;
             }
   
         }
     }
 }

When I use the commented out lines "transform.localPosition = sitting", this tiny script works to move the camera of the FPScharacter down as if sitting (and when the key is pressed, up again to standing).

When I try the Vector3.lerp version, it behaves erratically; pressing the key moves it down a little at a time, or up a little, or not at all.

What am I missing?

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 LazyElephant · Mar 24, 2016 at 08:29 PM 0
Share

In addition to the answer by Rob2309, lerp expects the 3rd parameter to be between 0 and 1. When it's 0, lerp returns the first parameter. When it's 1, lerp returns the 2nd parameter.

To lerp from one position to another, you have to start the third parameter at 0 and add incrementally to it every frame until it reaches 1.

I don't have time to do a complete answer, but you should check out the example usage in the unity scripting reference http://docs.unity3d.com/ScriptReference/Vector3.Lerp.html

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Rob2309 · Mar 24, 2016 at 06:12 PM

Hey :) The Vector3.Lerp function expects you to call it several times in order to reach the desired effect. In order for it to work properly, you have to call the Lerp function in every update call.

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 ledisnomad · Mar 24, 2016 at 06:14 PM 0
Share

O$$anonymous$$, so how do I call it several times? Could I put it in a while loop?

avatar image Rob2309 · Mar 25, 2016 at 02:46 AM 1
Share
 public $$anonymous$$eyCode k;
      private Vector3 standing = new Vector3(0.0f, 0.9f, 0.0f);
      private Vector3 sitting = new Vector3(0.0f, 0.4f, 0.0f);
      
      bool standing = true;
      
      // Update is called once per frame
      void Update()
      {
          if (Input.Get$$anonymous$$eyDown(k))
          {
             standing = !standing;
          }
          
          if(standing)
          {
             transform.localPosition = Vector3.Lerp(transform.localPosition, standing, Time.deltaTime);
          } else {
             transform.localPosition = Vector3.Lerp(transform.localPosition, sitting, Time.deltaTime);
          }
      }
  }

This code should do what you want to achieve... If not let me know and I'll give it another go :)

avatar image ledisnomad Rob2309 · Mar 25, 2016 at 12:35 PM 0
Share

Yes, works like a charm! Thank you, Rob2309. Seeing the implementation with the boolean makes sense.

avatar image Rob2309 ledisnomad · Mar 25, 2016 at 03:29 PM 0
Share

Glad I could help :)

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

55 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 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 rotating and object with Quaternion.Lerp 2 Answers

How to add force in random directions? 0 Answers

Attaching Light lerp to in-game clock 1 Answer

Mathf.Lerp Time problems. 1 Answer

Mathf.Lerp not working 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