Acceleration in Unity3D Character Scripts (Like Sonic the Hedgehog)

I want my character to accelerate from one speed to another, but I have no idea how to.
I plan on having a minimum speed (speed) a maximum speed (maxSpeed) and an amount of time to accelerate to max speed (accelTime).

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Move3D : MonoBehaviour
{
    public float speed;
    public float jump;
    protected bool newInput;
    public CharacterController move;
    public float maxSpeed;
    public float accelTime;
    private Vector3 moveDirection;
    public float gravity;

    void Start()
    {
        move = GetComponent<CharacterController>();
    }

    void LateUpdate()
    {
        float yStore = moveDirection.y;
        moveDirection = (transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal"));
        moveDirection = moveDirection.normalized * speed;
        moveDirection.y = yStore;

        if (move.isGrounded)
        {
            moveDirection.y = 0f;
            if (Input.GetButtonDown("Jump"))
            {
                moveDirection.y = jump;
            }
        }

        moveDirection.y = moveDirection.y + (Physics.gravity.y * gravity * Time.deltaTime);
        move.Move(moveDirection * Time.deltaTime);

    }
}

Any idea how I can insert it in here? Thanks. (P.S., this is my second thread for this, I couldn’t get back to the first.)

Generally you track a speed variable and gradually increase it over time.

You would multiply the movement by that speed.

Since this needs to actually be blended with the input from the player, it’s probably best to look at some existing tutorials for such a game. Fortunately there appears to be many choices to examine:

7036777--834121--Screen Shot 2021-04-13 at 5.32.55 PM.png

1 Like

Thanks, I’ll try this.