Class Engine
This class is used to create an engine for a vehicle, it is used to create your own engine for your car controller. It can be used in conjunction with the GearBox and Wheels classes to create a fully functional car controller.
Namespace: YACC
Assembly: .dll
Syntax
[Serializable]
public class Engine
Remarks
The engine is a standalone class and can be used without the GearBox and Wheels classes. However you need to take care to provide the wheelRPM and clutch to the engine yourself if you do so and if you want to use your own coded gearbox and wheels. Note that the class is serializable and can be used to set up the car directly in the inspector.
If you want to use the engine with the GearBox and Wheels classes you need to call the ConnectParts(GearBox, Wheels) method and pass the instances of the gearbox and wheels. This will allow the engine to calculate the torque output of the engine and other various parameters.
The engine requires few constraints in order to fully work:
- You need to set the throttle, this is a float value from 0 to 1, you can bind this with your controls
- You need to set the clutch, this is a float value from 0 to 1, the GearBox
already provides a dynamic value for you so you can associate with engine.clutch = gearbox.clutch;
- You need to set the wheelRPM, this is the RPM of the wheel(s) attached to the engine, this provides torque resistance to the engine, the RPM of the wheels need to be multiplied by the gear ratio
Examples
using YACC;
public class MyCustomCar : MonoBehaviour {
public Engine myEngine = new Engine();
public Engine myWheels = new Wheels();
public Engine myGearbox = new Engine();
// Setup the myEngine either in the inspector or in code:
void Start() {
myEngine.redlineRPM = 6000f;
myEngine.torqueCurve = new AnimationCurve(
new Keyframe(0, 0),
new Keyframe(0.15, 1),
new Keyframe(0.5, 0.6),
new Keyframe(0.95, 1),
new Keyframe(1, 0)
);
myEngine.motorPeakPower = 200f;
// Connect everything together
myEngine.ConnectParts(myGearbox, myWheels);
}
void Update() {
if (Input.GetKeyDown(KeyCode.Space)) {
StartCoroutine(myEngine.StartEngine());
}
if (Input.GetAxis("Vertical") > 0) {
myEngine.throttle = Input.GetAxis("Vertical");
} else {
myEngine.throttle = 0;
}
// Apply the engine torque to the wheels
myWheels.torque = myEngine.engineTorque * myGearbox.GetGearRatio();
}
}
Fields
Name | Description |
---|---|
accelerationRate | Acceleration rate of the engine in seconds from 0 to peak RPM. |
clutch | Sets the attached clutch, this comes usually from your gearbox and should match it's value. |
idleRPM | The desired RPM the engine will try to stay at when idling. |
motorPeakPower | Computed peak power of your engine (in KiloWatts). |
motorPeakTorque | Define the peak torque of your engine (in Newton-meter). |
oscillationRPM | A random value between 0 and this value will help in making the engine feel more realistic. |
powerCurve | The computed power output curve of your engine. |
redlineRPM | This is the redline RPM, over this RPM the engine won't generate any torque. |
state | Current state of the engine. See EngineState for the enums. |
throttle | Sets the throttle, value needs to be a float between 0 and 1. Use this to control the throttle valve of the engine. |
torqueCurve | Torque output curve of your engine from 0 to 1 (0 to 100% of your RPM) on the X axis where Y is the peak torque multiplier output. |
wheelRPM | The RPM of the wheel(s) attached to this engine, provides resistance to the engine. Remember to set this value at each update loop so your engine can react accordingly. |
Properties
Name | Description |
---|---|
RPM | Retrieve the current engine RPM. |
engineTorque | The torque output of the engine at the current RPM. |
maximumRPM | The maximum RPM the engine can reach, this is calculated by adding the redlineRPM and the idleRPM. |
Methods
Name | Description |
---|---|
ConnectParts(GearBox, Wheels) | Connects the engine to a gearbox and wheels, this is used to calculate the torque output of the engine and other various parameters. |
EvaluateTorqueCurveAtRPM(float) | Returns a value between 0 and 1 based on the current RPM and the torque curve. |
GetEngineLoadRatio() | Returns the ratio of the engine load in a range from 0 to 1. |
GetEngineTorqueAtRPM(float) | Returns the torque at the engine for a specific RPM. |
OnValidate() | Validates all fields, sets the maximumRPM and calculates the power curve. Make sure to call this method from your OnValidate method. |
StartEngine() | Exposes a coroutine to start the engine. |
StopEngine() | Exposes a coroutine to stop the engine. |