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
  4. Dismiss Notice

Question Acceleration in Unity3D Character Scripts (Like Sonic the Hedgehog)

Discussion in 'Scripting' started by MASTERAFF0, Apr 14, 2021.

  1. MASTERAFF0

    MASTERAFF0

    Joined:
    Aug 3, 2020
    Posts:
    5
    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).

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Move3D : MonoBehaviour
    6. {
    7.     public float speed;
    8.     public float jump;
    9.     protected bool newInput;
    10.     public CharacterController move;
    11.     public float maxSpeed;
    12.     public float accelTime;
    13.     private Vector3 moveDirection;
    14.     public float gravity;
    15.  
    16.     void Start()
    17.     {
    18.         move = GetComponent<CharacterController>();
    19.     }
    20.  
    21.     void LateUpdate()
    22.     {
    23.         float yStore = moveDirection.y;
    24.         moveDirection = (transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal"));
    25.         moveDirection = moveDirection.normalized * speed;
    26.         moveDirection.y = yStore;
    27.  
    28.         if (move.isGrounded)
    29.         {
    30.             moveDirection.y = 0f;
    31.             if (Input.GetButtonDown("Jump"))
    32.             {
    33.                 moveDirection.y = jump;
    34.             }
    35.         }
    36.  
    37.         moveDirection.y = moveDirection.y + (Physics.gravity.y * gravity * Time.deltaTime);
    38.         move.Move(moveDirection * Time.deltaTime);
    39.  
    40.     }
    41. }
    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.)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,779
    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:

    Screen Shot 2021-04-13 at 5.32.55 PM.png
     
  3. MASTERAFF0

    MASTERAFF0

    Joined:
    Aug 3, 2020
    Posts:
    5
    Thanks, I'll try this.