Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Slerping or Lerping something very slowly

Discussion in 'Scripting' started by omatase, Jul 1, 2016.

  1. omatase

    omatase

    Joined:
    Jul 31, 2014
    Posts:
    159
    I'm building fight sequences like you find in JRPGs with dynamic camera movement to better highlight the spell and character animations. I'm trying to pay attention to more details like dynamically changing depth of field to bring focus to other areas at points of the movement to doing something other than leaving the camera completely still when it rests to focus somewhere. So I'm trying to figure out the best way to pan the camera very very slowly over a few seconds. I'm having trouble doing it with Vector3.Slerp or Lerp, I can't figure out how to get it to move slowly enough. I pick a random direction to pan to that's a short distance away and tell it to Lerp or Slerp to that position. Once it reaches the random position I pick another random position and so on. It just gives the camera a bit of drift when it would otherwise be still. My question is, how can I make Unity Slerp or Lerp more slowly? Here is my code for the Slerp.

    Code (CSharp):
    1. transform.position = Vector3.Lerp(transform.position, _newLoiterPosition.Value, Mathf.Clamp01(Time.deltaTime * 7));
    I could just manually move in a random direction by adding a value to one of the coordinates and fully control the speed, but hoping there's a way to do this with S/Lerp.

    Thanks for the help!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You should not use Time.deltaTime in Lerp like that; among other issues, it makes the movement speed dependent on framerate (i.e. you will get different results with a low framerate vs. a high framerate). Instead advance t from 0.0 to 1.0 over time linearly, such as in a coroutine. http://wiki.unity3d.com/index.php?title=MoveObject See also here (read the whole thread).

    --Eric
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    If you want a constant, linear movement, simply use MoveTowards instead. That has a tendency to feel really mechanical though, so it's not always a good fit, but if you're moving really slowly, it could be the best bet.

    Also be sure to check out the "Lerp like a pro" link in my sig, it's got a lot of good info. Smoothstep could be a good fit for a camera that moves about, just to prevent the feel of a robot holding the camera.
     
    JoeStrout likes this.
  4. listener

    listener

    Joined:
    Apr 2, 2012
    Posts:
    179
    Last parameter in Lerp that you use Time.deltaTime * 7 actually makes nice effect like ease in but increments are less and less depending of distance from target.

    If you give lerp for that last parameter 0.5f it will be at middle between start and end 0.7f is 70% and so on...

    So one nice way is to use animation curves because you can traverse animation curve each frame for example and get its value and you can set it in editor very nicely to get ease in/out, linear or some other method of moving so no need for actually doing math (you can if you are interested how ease in etc.. works) since animation curves do that for you and gives you nice editor tool to change your behaviour, just use Evaluate to get value at certain point in time from the curve:

    http://docs.unity3d.com/ScriptReference/AnimationCurve.html
     
  5. omatase

    omatase

    Joined:
    Jul 31, 2014
    Posts:
    159
    Thank you, this was really helpful. Everywhere I go I see people using time as the final parameter to Slerp and Lerp and it's been so long since I went through the tutorials I don't remember specifically what they said about the two functions. This thread you recommended helped refresh my memory and gave me some good new info about it. I implemented the MoveObject for this and the code is way cleaner and everything works much better.