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 ErvinGamez · Jun 05, 2014 at 12:31 AM · cameramovementlerpsmooth

Problems with Lerp.

Hello! I am not an expert in coding but I can do simple stuff such as if statements and stuff like that, but then I thought I should learn to do more advanced things, so I made a little game that uses lerp, but I am having a problem with lerp, I am trying to make a game where if you click the ground you go down, and go back up in one second (Don't ask why), but it isn't smooth and it doesn't come back up when one second has passed, it instead goes down more. It is my first time using lerp by the way.

 var clicks = 0;
 var lerpPosition : float = 0.0f;
 var lerpTime : float = 5.0f;
 var start : Vector3 = new Vector3(0, 0.9070835, 0);
 var end : Vector3 = new Vector3(0, 0.1879287, 0);
 
 function OnMouseDown() {
 if (Input.GetKey ("mouse 0")) {
 print ("Push Up!");
 clicks = clicks + 1;
 lerpPosition += Time.deltaTime/lerpTime;
 Camera.main.transform.position = Vector3.Lerp(start,end,lerpPosition);
 yield WaitForSeconds(1);
 lerpPosition += Time.deltaTime/lerpTime;
 Camera.main.transform.position = Vector3.Lerp(end,start,lerpPosition);
 }
 }

Thanks for any help, also sorry for bad grammar, I am really tired.

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
0
Best Answer

Answer by robertbu · Jun 05, 2014 at 01:10 AM

  • Always use '#pragma strict' at the top of your files. It will prevent some inefficient code and also force some buts to be caught at compile time rather than runtime.

  • lerpPosition should be a local variable to OnMouseDown().

  • Your current code has a problem in that if the user clicks on object while the object is moving, then you will create two coroutines that will fight each other.

  • Lerp() needs to be executed across a number of frames. Right now, you only Lerp() twice per click (one up and one down).

Here is your code modified to fix these problems:

 #pragma strict
  
 var clicks = 0;
 var lerpTime : float = 5.0f;
 var start : Vector3 = new Vector3(0, 0.9070835, 0);
 var end : Vector3 = new Vector3(0, 0.1879287, 0);
 
 var lerping = false;
  
 function OnMouseDown() {
     if (!lerping) {
        lerping = true;
         var lerpPosition : float = 0.0f;
         print ("Push Up");
         clicks = clicks + 1;
         while (lerpPosition <= 1.0) {
             lerpPosition += Time.deltaTime/lerpTime;
             Camera.main.transform.position = Vector3.Lerp(start,end,lerpPosition);
             yield;
         }
         yield WaitForSeconds(1);
         lerpPosition = 0.0;
         print ("Pull Down");
 
         while (lerpPosition <= 1.0) {
             lerpPosition += Time.deltaTime/lerpTime;
             Camera.main.transform.position = Vector3.Lerp(end,start,lerpPosition);
             yield;
         }
         Camera.main.transform.position = start;
        lerping = false;
     }
 }
Comment
Add comment · Show 3 · 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 ErvinGamez · Jun 05, 2014 at 01:32 AM 0
Share

Hm, that kind of worked, it goes down, goes up and instantly goes down again. $$anonymous$$ight be my fault, I'll check if the positions are right.

avatar image robertbu · Jun 05, 2014 at 03:47 AM 0
Share

There was a bug on line 30. I fixed the script above.

avatar image ErvinGamez · Jun 05, 2014 at 03:20 PM 0
Share

That worked! Thank you so much!

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

21 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

Related Questions

Multiple Cars not working 1 Answer

How to smooth my camera (Vector3.Lerp) 0 Answers

Smooth Camera/Object Movement 1 Answer

Move A Camera Between Two Points Smoothly 1 Answer

Camera movement performs different with different inputs 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