How to write c# code 2d Car nitro , boost ?
void Start () {
rb = this.GetComponent<Rigidbody2D>();
// Set car rigidbody's COM
GetComponent<Rigidbody2D>().centerOfMass = centerOfMass.transform.localPosition;
// Starting with WheelJoint2D motor
motorBack = motorWheel.motor;
// Cast a ray to find isGrounded
StartCoroutine (RaycCast ());
EngineSoundS = GetComponent<AudioSource> ();
powerTemp = motorPower;
em = wheelParticle.emission;
em.enabled = false;
if (smoke) {
emSmoke = smoke.emission;
emSmoke.enabled = false;
}
}
float currentSpeed;
//float maxspeed = 300f;
void FixedUpdate(){
// speed limiter based on max speed limit value
if (speed > maxSpeed)
motorPower = 0;
else
motorPower = powerTemp;
// Moving forward
if (Input.GetAxis ("Horizontal") > 0 || HoriTemp > 0) {
// Add force to car back wheel
if(isGrounded)
motorBack.motorSpeed = Mathf.Lerp (motorBack.motorSpeed, -motorPower, Time.deltaTime * 1.4f);
// Wheel particles
if (isGrounded) {
if (speed < 4.3f) {
wheelParticle.transform.position = particlePosition.position;
em.enabled = true;
} else
em.enabled = false;
}
else
em.enabled = false;
}
else
{// Moving backward
if (Input.GetAxis ("Horizontal") < 0 || HoriTemp < 0) {
if (speed < -maxSpeed) {
if (isGrounded)
motorBack.motorSpeed = Mathf.Lerp (motorBack.motorSpeed, 0, Time.deltaTime * 3f);
} else {
if (isGrounded)
motorBack.motorSpeed = Mathf.Lerp (motorBack.motorSpeed, motorPower, Time.deltaTime * 1.4f);
}
} else {// Releasing car throttle and brake
if (isGrounded)
motorBack.motorSpeed = Mathf.Lerp (motorBack.motorSpeed, 0, Time.deltaTime * decelerationSpeed);
}
}
// Update WheelJoint2D motor inputs
motorWheel.motor = motorBack;
// Cheack fo rotate on the fly
Rotate ();
#if UNITY_EDITOR
EngineSoundEditor ();
#else
EngineSoundMobile ();
#endif
if (!isMobile)
HoriTemp = Input.GetAxis ("Horizontal");
if (useSmoke) {
if (Input.GetAxis ("Horizontal") > 0 || HoriTemp > 0) {
if (speed < smokeTargetSpeed)
emSmoke.enabled = true;
else
emSmoke.enabled = false;
} else
emSmoke.enabled = false;
}
}
Using the script you’ve provided, I would add a nitro input through Edit>ProjectSettings> Input, then either replacing one of the existing axes or adding another. Name it accordingly, and with the correct settings.
Next, within the above code, insert public float newMax; public float nitroPower; public float nitroBoostRate
, and a coroutine for the nitroboost IEnumerator nitroBoost();
, one that detects input from the key you chose, for instance
while(Input.GetKeyDown("Nitro") { maxSpeed = newMax; motorPower = Mathf.Lerp ( 14000, nitroPower, nitroBoostRate); yield return null; }
Then I would add a nitro deceleration rate public float nitroDecelRate
and do the same as before, for the key up after closing the while() within nitroBoost()
like so:
else{ if(Input.GetKeyUp("Nitro")) {maxSpeed = mathf.Lerp( newMax, 14f, nitroDecelRate); motorPower = Mathf.Lerp ( motorPower, 14000f, nitroDecelRate); }
Finally, you may consider adding an additional float for the nitroPowerDecel
for added adjustment.
This is all considering there isn’t already a method and input key for NOS somewhere in the rest of your script, as there is a bool NOS = false
and bool NOSActivation
.