- Home /
Is there a way to smoothly transition between two floats, with it slowing down towards the end
So I am making a helicopter sim in Unity, I was wondering, is there a way to smoothly transition between two floats,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Helicopter : MonoBehaviour
{
public bool engineOn;
public Rigidbody rb;
public float windDrag;
public float upForce;
public float gravity;
public Animator animator;
public Transform rotor;
public float pitch;
void Start()
{
rb.drag = windDrag;
}
void Update()
{
transform.rotation = Quaternion.Euler(pitch, 0f, 0f);
if (Input.GetKeyDown(KeyCode.E))
{
engineOn = !engineOn;
}
if (engineOn)
{
if (Input.GetKey(KeyCode.Space))
{
upForce += 40;
}
else if (!Input.GetKey(KeyCode.Space) && upForce > 0)
upForce -= 30;
if (Input.GetKey(KeyCode.W))
{
float addedPitch = pitch -= .3f;
pitch = Mathf.Lerp(pitch, addedPitch, Time.deltaTime);
}
if (Input.GetKey(KeyCode.S))
{
float addedPitch = pitch += .3f;
pitch = Mathf.Lerp(pitch, addedPitch, Time.deltaTime);
}
}
rb.AddForce(rotor.up * upForce);
SetBooleans();
}
void SetBooleans()
{
animator.SetBool("EngineOn", engineOn);
}
}
Here I wanna transition between the pitch and addedPitch variable, and kind of want it to go that when I when I hold down the W key, the pitch slowly goes up and starts to speed if I hold it long enough, and when I release W key, the incrementation of pitch slows down and eventually comes to a halt, where pitch is static. Mathf.Lerp didnt work well for me. Please help.
do you want that while you press the W key, it will increase the pitch infinitely or to some limit? And when you release the W key do you want the variable to decrease to a limit, or does it stop at the value that stopped?
Answer by jackmw94 · Jan 18, 2021 at 06:14 PM
There are a few issues with this part here (and the same goes for the same code on 'S')
float addedPitch = pitch -= .3f;
pitch = Mathf.Lerp(pitch, addedPitch, Time.deltaTime);
The first line takes pitch, reduces it by .3 then sets addedPitch to be the SAME value. So if pitch is 15, we reduce it to 14.7 then set added pitch to also be 14.7.
Given that pitch and added pitch are the same, when we lerp between them we'll always get that same value therefore the lerp is useless.
Also another note is that since you're passing Time.deltaTime which is always 1 divided by framerate, given that the lerp fraction should be between zero and one, it's always going to be effectively zero.
Now for the fix: If W and S just need to increase and decrease the pitch then you'll probably want to specify a maximum, minimum and change speed.
Now you can call:
if (Input.GetKey(KeyCode.W))
{
pitch -= pitchChangeSpeed * Time.deltaTime;
pitch = Mathf.Clamp(pitch, minimumPitch, maximumPitch);
}
Then same for S except changing -= to +=.
Also I've noticed that you don't use the pitch value anywhere within this code so unless you have another script that uses pitch then it won't work because it's never used!
Let me know how you get on!
How are you getting on with this problem @$$anonymous$$aggiethegsd ? Let me know if you need a hand implementing this or if this answer solved your problem, could you accept it? :)
Your answer
Follow this Question
Related Questions
Rotating one axis using Mathf.Lerp 1 Answer
Rigidbody object intersection 0 Answers
If a rigidbody is attached, should you always use AddTorque/AddRelative force for rotation/movement? 1 Answer
Simulator physics - how to get objects to "stick" to each other 0 Answers
How do I stop my player from very slowly sinking into the ground? 2 Answers