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
1
Question by Batzuga · May 21, 2014 at 12:46 AM · transformpositionvector3touchlerp

Lerp not working

I'm currently working on a menu. And I need to get it to slide. But my Lerp is working just like normal transform.position and I don't really know why. Heres the code I'm using! Any ideas?

var player : Transform;

private var fp : Vector2;

private var lp : Vector2;

var startp = Vector3(0,0,-5);

var pos1 = Vector3(5,0,-5);

function Start() { }

function Update() {

 for (var touch : Touch in Input.touches)
 {
       if (touch.phase == TouchPhase.Began)
       {
             fp = touch.position;
             lp = touch.position;
       }
       if (touch.phase == TouchPhase.Moved )
       {
             lp = touch.position;
       }
       if(touch.phase == TouchPhase.Ended)
       { 
   
       if((fp.x - lp.x) > 0.5) // left swipe
       {
                
               player.transform.position = Vector3.Lerp(startp, pos1, Time.deltaTime);
                    
       }
       else if((fp.x - lp.x) < -0.5) // right swipe
       {
            
               player.transform.position = Vector3.Lerp(pos1, startp, 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

1 Reply

· Add your reply
  • Sort: 
avatar image
5

Answer by rutter · May 21, 2014 at 12:51 AM

Oddly enough, I'm currently writing a blog post about this exact question. I'll drop the first section, here, since it's pretty much ready to go:

I see this sort of thing far too often:

 transform.position = Vector3.Lerp(startPos, endPos, Time.deltaTime);

The person posting it is usually convinced that Vector3.Lerp is "broken", but the real problem is that they're not using it correctly.

Lerp, short for "linear interpolation" does one very simple thing: given two values, x and y, it returns a value that is t percent between them. If you expect the output to change, the arguments you pass in need to reflect that!

In the example above, it doesn't make sense to just pass in Time.deltaTime, because that's only the time that passed during the most recent frame. If your game is running at a constant 50fps, that's always going to be 0.02.

We'll get better results if we let that timer accumulate over multiple frames.

Something like this should get the point across:

 public class LerpExample : MonoBehaviour {
     float lerpTime = 1f;
     float currentLerpTime;
     
     float moveDistance = 10f;
     
     Vector3 startPos;
     Vector3 endPos;
     
     protected void Start() {
         startPos = transform.position;
         endPos = transform.position + transform.up * moveDistance;
     }
     
     protected void Update() {
         //reset when we press spacebar
         if (Input.GetKeyDown(KeyCode.Space)) {
             currentLerpTime = 0f;
         }
         
         //increment timer once per frame
         currentLerpTime += Time.deltaTime;
         if (currentLerpTime > lerpTime) {
             currentLerpTime = lerpTime;
         }
         
         //lerp!
         float perc = currentLerpTime / lerpTime;
         transform.position = Vector3.Lerp(startPos, endPos, perc);
     }
 }
 

You could create an empty scene, drop in a cube, attach this script, and see it in action.

Notice the result if you change lerpTime or moveDistance.

Comment
Add comment · Show 5 · 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 Batzuga · May 21, 2014 at 12:18 PM 0
Share

That works yes, but when I try to convert It into the javascript and be affected only when i swipe, it still acts like normal transform.position

avatar image KevinKLV · Feb 09, 2017 at 02:54 AM 0
Share

Thanks @rutter, this helped me to understand better Time.detalTime and fix a bug. Cheers

avatar image Eco-Editor · Jul 16, 2017 at 10:18 AM 0
Share

Hello @rutter I also have a problem with Lerp, but I understood it differently. In a line such as:

 shopper.transform.forward = Vector3.Lerp(shopper.transform.forward, SceneIndex.instance.waypointTransforms[nextWayPoint].targetTransform.forward, 8f * Time.deltaTime

What will happen? Thanks

avatar image Bunny83 Eco-Editor · Jul 16, 2017 at 10:23 AM 0
Share

Your "shopper" object will rotate in a non linear fashion towards the forward direction of your target waypoint (the blue axis of that waypoint object).

avatar image Eco-Editor Bunny83 · Jul 16, 2017 at 10:46 AM 0
Share

What does it mean non linear?

If its written like that, will it be able to complete the transform.forward before moving on the the next function?

 void Update(){
 shopper.transform.forward = Vector3.Lerp(shopper.transform.forward, SceneIndex.instance.waypointTransforms[nextWayPoint].targetTransform.forward, 8f * Time.deltaTime)
 ToReachPointState();
 }




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

24 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

Related Questions

How to make your object follow your touch position faster or instantaneously??? 2 Answers

Vector3.Lerp works outside of Update() 3 Answers

Unity Car Accelerometer 0 Answers

Smooth movement like Lerp with Touch Input? 2 Answers

Setting target position in Vector3.MoveTowards 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