Animation Curve and Motor Torque

Hello,

Please help me, I searched everywhere, but I didn’t find a solution. My problem is that I use a Animation Curve for engine RPM, but I think that my script doesn’t read that curve or something like that. I followed this tutorial for my script Entity Crisis: Unity3D WheelCollider and motorTorque.

Also my engine RPM is 800 and after I press W (vertical input is positive) my engine RPM is smaller. Is this normal? Please help me, I appreciate your time and thank you in advance!

Have you got any tutorial or something that can help me?

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

public enum Axel
{
    Front,
    Rear
}

public enum PositionWheel
{
    Left,
    Right
}

[System.Serializable]
public struct Wheel
{
    public GameObject modelWheel;
    public WheelCollider colliderWheel;
    public Axel axel;
    public PositionWheel positionWheel;
}

public class CarController : MonoBehaviour
{
    [Header("Engine")]
    public AnimationCurve torqueCurve;
    public AnimationCurve gearRatios;
    public float finalDriveRatio = 3.6f;
    public float minRPM = 800f;
    public float wheelRPM;
    public float gearIndex;
    public float totalMotorTorque;
    public float motorRPM;

    private float forwardAccel, reverseAccel;
    private float xAxis, yAxis;

    [SerializeField] private List<Wheel> wheels;
    private Speedometer speedometer;

    void Start()
    {
        speedometer = GameObject.FindObjectOfType<Speedometer>();
    }

    void Update()
    {
        GetInput();
        AnimateWheels();
        CalculateEngineRPM();
    }

    void FixedUpdate()
    {
        UpdateRotation();
        Acceleration();
        Turn();
    }

    private void GetInput()
    {
        xAxis = Input.GetAxis("Horizontal");
        yAxis = Input.GetAxis("Vertical");
    }

    private void UpdateRotation()
    {
        transform.rotation = Quaternion.Euler(0, 0, 0);
    }

    void Turn()
    {
        float xOffset = xAxis * 2f * Time.deltaTime;
        float newXpos = transform.position.x + xOffset;

        transform.position = new Vector3(newXpos, transform.position.y, transform.position.z);
    }

    private void CalculateEngineRPM()
    {
        CalculateWheelRPM();

        motorRPM = minRPM + (wheelRPM * finalDriveRatio * gearRatios.Evaluate(gearIndex));
    }

    private void CalculateWheelRPM()
    {
        foreach(var wheel in wheels)
        {
            wheelRPM = wheel.colliderWheel.rpm;
        }
    }

    private void Acceleration()
    {
        forwardAccel = 10000f * 100f;

        totalMotorTorque = torqueCurve.Evaluate(motorRPM) * gearRatios.Evaluate(gearIndex) * finalDriveRatio * forwardAccel;

        foreach (var wheel in wheels)
        {
            wheel.colliderWheel.motorTorque = yAxis * totalMotorTorque * Time.deltaTime;
        }
    }

    private void AnimateWheels()
    {
        foreach (var wheel in wheels)
        {
            Quaternion _rot;
            Vector3 _pos;
            wheel.colliderWheel.GetWorldPose(out _pos, out _rot);
            wheel.modelWheel.transform.rotation = _rot;
        }
    }
}

7049545--836263--Capture.PNG 7049545--836260--Capture1.PNG

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

If you are running a mobile device you can also see the console output. Google for how.

As for tutorials there are a LOT out there. You just need to search with various terms, such as:

7050337--836422--Screen Shot 2021-04-17 at 11.09.36 AM.png

1 Like

Thank you for your answer. I think my problem is that engine rpm doesn’t covert fine values from wheel rpm, maybe the problem is this line motorRPM = minRPM + (wheelRPM * finalDriveRatio * gearRatios.Evaluate(gearIndex));
and the motor torque isn’t depending on Animation Curve… but I don’t know what is wrong