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 wannamake · Aug 19, 2012 at 07:51 PM · lerpsmoothonmousedown

something wrong with vector3.Lerp.

I just made a simple code. What I try to do is that if I click on a button object, 'the Thing' moves from pointA to pointB smoothly. And then I click the button again? Yes, 'the thing' moves from pointB to pointA.

Just simple thing. I made code and applied on the button object. After all, something strange happened. I know 'Vector3.Lerp' make an object move smoothly. But, in this case, 'the Thing' just moved from pointA to pointB. Like teleport. Not smooth movement. Why is that happen? What I did wrong?

var pointA : Transform;

var pointB : Transform;

var looktarget : Transform;

var theThing : Transform;

var moveState = 0;

function OnMouseDown(){

if(moveState == 0){

moveState = 1;

theThing.transform.position = Vector3.Lerp(pointA.position,pointB.position,Time.time);

theThing.transform.LookAt(looktarget.position);

}

else if(moveState == 1){

moveState = 0;

theThing.transform.position = Vector3.Lerp(pointB.position,pointA.position,Time.time);

theThing.transform.LookAt(looktarget.position);

}

Debug.Log("moveState is "+moveState);

}

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 Eric5h5 · Aug 19, 2012 at 09:27 PM 0
Share

Format code when you use it in a question, please.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by tigerfoot · Aug 20, 2012 at 02:18 AM

Time in Lerp function is clamped between 0 and 1. 0 means your object is at starting position and 1 means your object is at target position. Therefore you must create a variable which you will increment from 0 to 1 and put that in Lerp and not Time.time or Time.deltaTime.

If you put Time.deltaTime your object will be somewhere around middle between starting position and target position. It will never reach your goal, unless your FPS drops to 0 or 1 FPS.

THIS will explain it to you much better than me.

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 TheVectorHunter · Aug 20, 2012 at 02:57 AM 0
Share

You were wrong, I was right. $$anonymous$$ultiplying a variable by Time.deltaTime makes it framerate independent.

avatar image Eric5h5 · Aug 20, 2012 at 03:35 AM 0
Share

However, using it alone as the third parameter in Lerp generally does not accomplish what you want. tigerfoot is pretty much correct here. Aside from the link posted, see this: http://answers.unity3d.com/questions/14288/can-someone-explain-how-using-timedeltatime-as-t-i.html

avatar image
0

Answer by TheVectorHunter · Aug 19, 2012 at 08:41 PM

What you did wrong was you used 'Time.time'. 'Time.time' could be 100 seconds in by the time you use it which means it would immediately place you at your destination.

http://docs.unity3d.com/Documentation/ScriptReference/Time-time.html

What you need to use is this:

http://docs.unity3d.com/Documentation/ScriptReference/Time-deltaTime.html

Make a variable that is incremented/decremented and then multiplied by 'Time.deltaTime' so it moves by seconds not frames.

Comment
Add comment · Show 7 · 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 tigerfoot · Aug 19, 2012 at 10:33 PM 0
Share

It can never be 100 because time in Lerp is clamped between 0 and 1.

And that is not the speed of movement. 0 means starting position and 1 means target position. Time.deltaTime will not get the object to reach target position.

avatar image TheVectorHunter · Aug 19, 2012 at 11:33 PM 0
Share

That was not what I said read my answer thoroughly before you criticize it.

avatar image wannamake · Aug 20, 2012 at 12:38 AM 0
Share

Thank you for your answer but i couldn't solve the problem...Yeah i already tried several ways include 'Time.deltaTime.'.... And there's another mystery happened..haha.. The object(theThing)'s speed was not changed but final position was changed a little bit...it means theThing teleported to the other position.(not pointB.) i think time/timedeltatime thing is not important. i wonder if 'vertor3.Lerp' dosen't work under 'function On$$anonymous$$ouseDown'.

avatar image TheVectorHunter · Aug 20, 2012 at 01:58 AM 0
Share

Lmfao, I didn't notice that it was in On$$anonymous$$ouseDown()... that is a one time function/method call, it is only called once upon the user clicking the mouse.

avatar image TheVectorHunter · Aug 20, 2012 at 02:01 AM 0
Share

To fix this problem you should have a coroutine setup that will be called upon the On$$anonymous$$ouseDown(), and will cause 'theThing' to move for however long you want the motion to take.

Show more comments

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

12 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

Related Questions

Move an object by clicking on another 3 Answers

Slerp / lerp not creating a smooth transition 2 Answers

How to move an object from point A to point B with one key press 3 Answers

Smoothing motion with vector3 lerp 3 Answers

interpolate a move variable 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