How can I simulate car Acceleration with gear system?

I don`t want use WheelCollider because my game is so simple.

I am using _rb.AddForce(transform.forward * forwardForce, ForceMode.Acceleration); for acceleration.This is work for what i want.

I am using public int[] gears = {36,72,108,144,180}; for declare my gears

How can I want switch the gear when car reach the current gear`s max speed with 2 second delay between current gear and next gear?

Not sure what you want to do with gears or what function do they have. From what I can understand, you want an INDICATOR of speed… Maybe you can multiply the current gear by the forward speed to make the car faster with each gear… something like this

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

[RequireComponent(typeof(Rigidbody))]
public class Gears : MonoBehaviour
{
    public List<int> _gears = new List<int> { 0, 36, 72, 108, 144, 180 };
    public float _gearsTransitionSeconds = 2;
    public float _forwardForce = 10;


    private Rigidbody _rb;
    private bool _transition = false;
    private int _currentGear = 0;
    private float _speed = 0;

    void Start() {
        _rb = GetComponent<Rigidbody>();
    }


    void Update() {      
        _speed = _rb.velocity.sqrMagnitude/2; //get speed
        if (_currentGear != _gears.Count-1) { //is not last gear
            if (_speed >= _gears[_currentGear + 1] && !_transition) { //has reached gear limit
                StartCoroutine(SwitchGearTransition()); //do the gear switching transition
            }
        }

        if (!_transition) {
            Accelerate();
        }
    }

    private IEnumerator SwitchGearTransition() {
        _transition = true;
        yield return new WaitForSeconds(_gearsTransitionSeconds);
        _currentGear ++;
        _transition = false;
    }

    private void Accelerate() {
        int _gearFactor = _currentGear == 0 ? 1 : _currentGear;
        _rb.AddForce(Vector3.forward * _forwardForce * _gearFactor, ForceMode.Acceleration);
    }
}
1 Like

You have an array of integers. What do you want to do with them? What property of your acceleration do you want to change? What effects are you looking to get?

Until you answer exactly how you want those magic numbers to affect your forwardForce (or anything else), you won’t be able to develop a solution really.

1 Like

This is exactly what I want. Thank you.

1 Like