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 Testing · Oct 08, 2011 at 11:42 PM · transformtimelerpmathf

Mathf.Lerp Question

Hi, a really noob question, i got this code from the unity manual

// Fades from minimum to maximum in one second

var minimum = 10.0; var maximum = 20.0;

function Update () { transform.position = Vector3(Mathf.Lerp(minimum, maximum, Time.time), 0, 0); }

Now how would u make it instead of one second say 3 or 4? I tried Time.deltaTime * 4 but the results are weird.

EDITED: My goal is to move a gun (FPS) to the center of the screen when we zoom in roughly at the same speed as the cam FOV decreases then return it to the original position.

I have this code so far:

function LateUpdate () { if (Input.GetButtonDown ("Fire2")){ pressing = true; } if (Input.GetButtonUp ("Fire2")){ pressing = false; } // Calculate the desired distance if (pressing){ camera1.fieldOfView = Mathf.Lerp(camera1.fieldOfView, zoomFOV, Time.deltaTime * lerpSpeed); gun.localPosition = Vector3(Mathf.Lerp(minimum, maximum, Time.time ), -0.2390689, 0.4984837); DOF.enabled = true; } else if (!pressing) { camera1.fieldOfView = Mathf.Lerp(camera1.fieldOfView, normalFOV, Time.deltaTime * lerpSpeed);

 gun.localPosition = Vector3(Mathf.Lerp(maximum, minimum, Time.time ), -0.2390689, 0.4984837); 
 DOF.enabled = false; 

}

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 cregox · Nov 22, 2012 at 08:55 PM 0
Share

better understanding lerp and deltatime: http://answers.unity3d.com/questions/14288/can-someone-explain-how-using-timedeltatime-as-t-i.html?sort=oldest

2 Replies

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

Answer by aldonaletto · Oct 08, 2011 at 11:50 PM

This is a bad example, because it uses Time.time - it starts counting when the game begins, and never returns to zero, so you will only see this working once. It's better to do something like this:

var minimum = 10.0; var maximum = 20.0; var duration: float = 4; // duration in seconds var timer: float = 0; // set this to zero to repeat the cycle

function Update () {

 transform.position = Vector3(Mathf.Lerp(minimum, maximum, timer), 0, 0); 
 timer += Time.deltaTime/time; // add time elapsed since last Update

}

EDITED: You can do that in a simpler manner. Since Mathf.Lerp(from, to, t) returns a value between from and to proportional to t, you can increment t from 0 to 1 when Fire2 is pressed, and return to 0 when it's released. This can be done by adding or subtracting Time.deltaTime/duration from the variable t. The variable t is clamped between 0 and 1, so when you press or release Fire2 t always take the same time to go or return.

var t: float = 0; var duration: float = 3;

function LateUpdate () { if (Input.GetButton("Fire2")){ // if fire2 pressed... DOF.enabled = true; // enable DOF t += Time.deltaTime/duration; // t goes towards 1 } else { // if fire2 not pressed... DOF.enabled = false; // disable DOF t -= Time.deltaTime/duration; // t goes towards 0 } t = Mathf.Clamp(t, 0, 1); // keep t between 0 and 1 camera1.fieldOfView = Mathf.Lerp(normalFOV, zoomFOV, t); gun.localPosition.x = Mathf.Lerp(minimum, maximum, t); }

Comment
Add comment · Show 4 · 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 Testing · Oct 09, 2011 at 01:00 AM 0
Share

Hi, thank you 4 the quick reply. $$anonymous$$y goal is to move a gun (FPS) to the center of the screen when we zoom in roughly at the same speed as the cam FOV decreases then return it to the original position.

I have this code so far:

function LateUpdate () {

if (Input.GetButtonDown ("Fire2")){ pressing = true; } if (Input.GetButtonUp ("Fire2")){ pressing = false; } // Calculate the desired distance if (pressing){ camera1.fieldOfView = $$anonymous$$athf.Lerp(camera1.fieldOfView, zoomFOV, Time.deltaTime * lerpSpeed);

 gun.localPosition = Vector3($$anonymous$$athf.Lerp($$anonymous$$imum, maximum, Time.time ), -0.2390689, 0.4984837);
 DOF.enabled = true;

} else if (!pressing) { camera1.fieldOfView = $$anonymous$$athf.Lerp(camera1.fieldOfView, normalFOV, Time.deltaTime * lerpSpeed);

gun.localPosition = Vector3($$anonymous$$athf.Lerp(maximum, $$anonymous$$imum, Time.time ), -0.2390689, 0.4984837); DOF.enabled = false; }

avatar image Fattie · Oct 09, 2011 at 07:20 AM 0
Share

@Testing, simply EDIT your original question and put all the code, etc, in there.

avatar image Wolfram · Feb 04, 2013 at 12:16 PM 1
Share

Sheesh, I'd wish they'd fix the Lerp examples already, they're useless - or have been put there on purpose to confuse newbies :-(

avatar image Bunny83 · Feb 04, 2013 at 12:19 PM 0
Share

Yes, the examples can only be understood by those who know already how it works.

avatar image
0

Answer by Patel-Sagar · Feb 04, 2013 at 12:49 PM

I do it this simple way..

float temp; bool lerpStarted; float noOfSeconds;

void callThisToStartLerp() { temp = Time.time; lerpStarted = true; }

void Update() { if(lerpStarted) { transform.position = Vector3(Mathf.Lerp(minimum, maximum, (Time.time - temp)/noOfSeconds), 0, 0); } }

In this Way, You can use lerp for any no. of seconds defined in noOfSeconds. and you can use yield to false the bool var lerpStarted.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to lerp in different states in a switch statement 0 Answers

Move Transform to Target in X seconds 3 Answers

Jump with mathf.lerp problem 2 Answers

how to use time on lerp 2 Answers

Clamping negative and positive values. 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