- Home /
How do I make GameObject actions ease in and out
I'm trying to make a car game with a forward and reverse mechanic that ease into its max speed and ease out into a stop. Current code: (Everything labeled)
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Movement : MonoBehaviour {
public Rigidbody rb;
public bool grounded = true;
void OnCollisionEnter(UnityEngine.Collision collisionInfo)
{
grounded = true;
}
void OnCollisionExit(UnityEngine.Collision collisionInfo)
{
grounded = false;
}
//Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float MaxForwardSpeed = 20f; // maximum speed for acceleration and reverse
float TurningRadius = 80f; // maximum speed car can turn
float Speed = 0; // variable that changes the speed
float Jump = 10f; // not important
if (grounded == true) //on ground
{
if (Input.GetKey(KeyCode.W)) // acceleterate
{
// ATTEMPTED GRADUAL ACCELERATION /////////////////////////////////////////////////////
if ((Speed < MaxForwardSpeed))
{
Speed += 2;
transform.position += transform.forward * Time.deltaTime * Speed;
}
Debug.Log(Speed);
///////////////////////////////////////////////////////////////////////////////////////
if (Input.GetKey(KeyCode.A)) // only turns when going forward
{
transform.Rotate(0, -TurningRadius * Time.deltaTime, 0);
}
if (Input.GetKey(KeyCode.D))
{
transform.Rotate(0, TurningRadius * Time.deltaTime, 0);
}
}
if (Input.GetKey(KeyCode.S)) // reverse
{
transform.position -= transform.forward * Time.deltaTime * MaxForwardSpeed;
if (Input.GetKey(KeyCode.A)) // only turns when going backward
{
transform.Rotate(0, -TurningRadius * Time.deltaTime, 0);
}
if (Input.GetKey(KeyCode.D))
{
transform.Rotate(0, TurningRadius * Time.deltaTime, 0);
}
}
//if (Input.GetMouseButtonDown(1))
if (Input.GetKeyDown(KeyCode.Space)) // jump function
{
rb.AddForce(transform.up * Jump, ForceMode.Impulse);
}
if (Input.GetKey(KeyCode.LeftShift)) // air roll toggle(hold)
{
if (Input.GetKey(KeyCode.A)) // drift left
{
transform.Rotate(0, -1, 0);
}
if (Input.GetKey(KeyCode.D)) // drift right
{
transform.Rotate(0, 1, 0);
}
}
}
if (grounded == false) //off ground
{
if (Input.GetKey(KeyCode.W)) // push forward
{
transform.Rotate(1, 0, 0);
}
if (Input.GetKey(KeyCode.A)) // car turns left in air
{
transform.Rotate(0, -1, 0);
}
if (Input.GetKey(KeyCode.S)) // pull back
{
transform.Rotate(-1, 0, 0);
}
if (Input.GetKey(KeyCode.D)) // car turns right in air
{
transform.Rotate(0, 1, 0);
}
if (Input.GetKey(KeyCode.LeftShift)) // air roll toggle(hold)
{
if (Input.GetKey(KeyCode.A)) // air roll left
{
transform.Rotate(0, 1, 0); // counters other "(KeyCode.A) movement"
transform.Rotate(0, 0, 1);
}
if (Input.GetKey(KeyCode.D)) // air roll right
{
transform.Rotate(0, -1, 0); // counters other "(KeyCode.D) movement"
transform.Rotate(0, 0, -1);
}
}
}
}
}
sorry so much code, I've been working on this for hours and just started using unity a day ago (don't know how to make more compact code)
Answer by theratboy · Oct 24, 2019 at 11:38 PM
Look into lerp. There's also settings called gravity under edit>project settings>input. Gravity makes a input go from 0 (off) to 1 (on) more slowly or fast. if you want smoother movement change your getkey to getaxis and customize the gravity settings from the project settings page in the editor.