- Home /
Question by
HexStudio · Sep 08, 2020 at 09:23 PM ·
caranimationcurvetorquecar gamecar physics
Engine animation curve
Hello, i'm making a racing game with only EVs, right now, I have the torque in a float value. But i want to use a animation curve in which the torque will evoluate according to the engine RPM (like you get 700 pound/feet of torque from 0 to 1000 rpm but from 1000 to 2000 it drops to 400 pound/feet of torque). So the torque is on the vertical axis and the rpm is on the horizontal axis. PS : i also need a simple way to calculate the wheel rpm. There is the script :
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
public enum Axel
{
front,
rear
}
[Serializable]
public struct Wheel
{
public GameObject model;
public WheelCollider collider;
public Axel axel;
}
[RequireComponent (typeof(InputManager))]
public class CarController : MonoBehaviour
{
public InputManager inputManager;
public List<Wheel> wheels;
public Rigidbody localRigidbody;
[Header("Throttle system")]
public float torque;
public float torqueMultiplier = 1;
public float finalTorque;
public float level = 1;
public float currentSpeed;
public float topSpeed;
[Header("Steering system")]
public float maxSteerAngle = 20;
public float responsiveness = 1;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
finalTorque = torque * torqueMultiplier;
currentSpeed = localRigidbody.velocity.sqrMagnitude;
ThrottleSystem();
SteeringSystem();
AnimateWheels();
}
void ThrottleSystem()
{
foreach(var wheel in wheels)
{
if (currentSpeed > topSpeed)
{
wheel.collider.motorTorque = 0;
}
else if (currentSpeed < topSpeed)
{
wheel.collider.motorTorque = ((inputManager.throttle * finalTorque) * level) / 4;
}
}
}
void SteeringSystem()
{
foreach (var wheel in wheels)
{
if (wheel.axel == Axel.front)
{
wheel.collider.steerAngle = inputManager.steer * maxSteerAngle * responsiveness;
}
else
{
wheel.collider.steerAngle = 0;
}
}
}
void AnimateWheels()
{
foreach (var wheel in wheels)
{
Vector3 wheelColliderPosition;
Quaternion WheelColliderRotation;
wheel.collider.GetWorldPose(out wheelColliderPosition, out WheelColliderRotation);
wheel.model.transform.position = wheelColliderPosition;
wheel.model.transform.rotation = WheelColliderRotation;
}
}
}
Comment
Answer by rautudenis · Apr 17, 2021 at 12:27 PM
Is a simple way to calculate wheel rpm in your case
wheel.collider.rpm
Your answer
Follow this Question
Related Questions
Shift gear with own RPM? Help please! 0 Answers
Extremely simple car movement 1 Answer
Better RPM?? 0 Answers
transmission clutch help! 1 Answer
Car Gearbox script 1 Answer