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;
}
}
}

