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 Cool Dave · Dec 06, 2012 at 12:28 AM · cameralerptranslate

Is Lerp acting weird?

Here is my code.

 #pragma strict
 #pragma implicit
 #pragma downcast
 
 public var theCamera : Transform;
 
 function OnMouseUp()
 {
 theCamera.position = Vector3.Lerp(theCamera.position, transform.position, Time.time);
 }

Instead of moving nicely over 1 second, it moves instantly. This script is attached to a Gameobject (with a collider), not a GUI Object.

Am I doing something wrong? Or is there a glitch?

Thanks in advance.

Dave

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

2 Replies

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

Answer by Eric5h5 · Dec 06, 2012 at 12:44 AM

OnMouseUp only runs once, when the mouse button is released. Lerp is not a function that "runs over time" or anything, it's just a very simple math function that returns a value immediately. (Specifically, (b - a) * t + a, where t is clamped between 0 and 1.) Also you should not use Time.time in the 3rd parameter, since it needs a value between 0 and 1.

If you want movement over time, it's generally easiest to use a coroutine, like this.

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 Bunny83 · Dec 06, 2012 at 12:46 AM 0
Share

:D Damn, i totally missed the On$$anonymous$$ouseUp ....

avatar image Cool Dave · Dec 06, 2012 at 02:14 AM 0
Share

So what you're saying is that

  1. I shouldn't use Lerp in On$$anonymous$$ouseUp

  2. I shouldn't use theCamera.position, I should only reference static objects like pointTo$$anonymous$$oveTo.position

  3. I shouldn't put Time.time in the 3rd parameter

  4. I shouldn't use Lerp in this case anyways

Is this right?

avatar image Eric5h5 · Dec 06, 2012 at 02:24 AM 0
Share

Nope, the only thing I said is #3. Please click on the link I provided about using a coroutine.

avatar image Cool Dave · Dec 06, 2012 at 02:40 AM 0
Share

I mean Bunny83 and you together seemed to say those four.

Perhaps I'm thick, but that 'coroutine script' is much too long...I only want one, maybe two lines of code, preferably that I can insert in my On$$anonymous$$ouseUp. Could (not may) I use a line or two of your script and make it work?

I really don't want anything complicated for this application.

avatar image Eric5h5 · Dec 06, 2012 at 02:59 AM 0
Share

I don't think you've actually read that page. You put the script in your project, and then call the functions as needed (with one line of code). Please at least look at the example usage code. Every line of code in the $$anonymous$$oveObject script is necessary for it to work as documented.

Show more comments
avatar image
1

Answer by Bunny83 · Dec 06, 2012 at 12:43 AM

You do a lot worng ;) Lerp is ment to interpolate between two fix positions / values. You use the current position as start value.

So for example when your camera is at 0,0,0 at the beginning and your target position is at 100,0,0 it will process like this:

 Frame   start     end    t     resulting value
 0       0         100    0     0
 1       0         100    0.05  5
 2       5         100    0.1   14.5
 3       14.5      100    0.15  27.325
 4       27.325    100    0.2   41.86
 5       41.86     100    0.25  56.395

You need to use the same start and end position

 Frame   start     end    t     resulting value
 0       0         100    0     0
 1       0         100    0.05  5
 2       0         100    0.1   10
 3       0         100    0.15  15
 4       0         100    0.2   20
 5       0         100    0.25  25


Lerp is not black magic. It's very very simple. That's how it's implemented:

 function Lerp(from : Vector3, to : Vector3, t : float)
 {
     return from + (to - from) * Mathf.Clamp01(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

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

11 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

Related Questions

I'm using TRANSLATE but want to use LERP. C# 2 Answers

How do I translate around a circle? 3 Answers

Why my camera rotates around gameobject 1 Answer

How to move camera from point A to B 1 Answer

Camera moving in one direction? 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