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 smirlianos · Oct 05, 2013 at 06:59 PM · vector3lerpvector3.lerpy axishole

Vector3.Lerp isn't working

Hello,

An enemy is appearing in a random position every to seconds, and it's supposed to hame a smooth movement, to add +1 to it's y axis. Like a mole that goes out of its hole!

But it doesn't work! The enemy bumps like a shaker for ever! What's wrong?

 #pragma strict
 private var curPos : Vector3;
 private var newPos : Vector3;
 
 function Start () {
     curPos = transform.position;
     newPos = new Vector3(transform.position.x, transform.position.y + 2, transform.position.z);
 }
 
 function Update () {
     transform.position = Vector3.Lerp(curPos, newPos, 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

3 Replies

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

Answer by robertbu · Oct 05, 2013 at 07:44 PM

When using 'Time.deltaTime' in a Lerp(), you need to update the current position each frame, not in Start:

 #pragma strict
 var speed = 0.75;
 private var newPos : Vector3;
  
 function Start () {
     newPos = new Vector3(transform.position.x, transform.position.y + 2, transform.position.z);
 }
  
 function Update () {
     transform.position = Vector3.Lerp(transform.position, newPos, Time.deltaTime * speed);
 }

I've added 'speed' so you can control the speed of the movement. This will create an eased movement towards the end. If you don't want an eased movement, replace the Vector3.Lerp() with Vector3.MoveTowards() and adjust 'speed'.

Many of the other solutions will work, but I wanted to correct the perception that you had to put in values ranging from 0 to 1 to get Lerp() to work. Using deltaTime like this is a non-traditional use of Lerp(), but it is very common in Unity.

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 DarkPixel · Oct 05, 2013 at 07:24 PM

Vector3.Lerp

As the doc says, it used to interpolate between 2 vector, the last parameter (t) need to be between 0 and 1.

You probably need to use a member in your class to store the current T value. And in the update:

 void Update()
 {
     if (T > 1.0f)
        return;
 
     T = T + ((speedPerSecond * Time.deltaTime) / distanceBetweenThe2Pos);
     transform.position = Vector3.Lerp(curPos, newPos, T);
 }


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 meat5000 · Oct 05, 2013 at 07:36 PM

At 30 FPS one frame is 0.033 Seconds. Your Lerp will have your transform.position move once to a position very very close to curPos. This is because deltaTime is roughly the same every frame and t = 0.03 represents 3% of your movement. Lerp works with t between 0 and 1. 0 represents curPos, 1 represents newPos and values between 0 and 1 will scale your Lerp between curPos and newPos, so t = 0.5 is halfway between the two.

Make your own timer

timer = 0;

timer += Time.deltaTime;

Use it to Lerp then reset it.

If lerpTime = 5; // Desired time to complete lerp

timer/lerpTime = 1 when timer = 5. //At t=1 Lerp is complete.

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.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

make my gameobject lerp between points more smoothly 0 Answers

vector3 lerp based on 2 different floats/axes? 1 Answer

Vector3.Lerp move weapon up to players face 2 Answers

Vector3.Lerp completing before t = 1? 2 Answers

Vector3.Lerp moves in the wrong direction 1 Answer


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