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.