One function cancels another...can't figure it out!

Hi!
I can’t figure this one out just yet…I have 2 functions which I both call in FixedUpdate, “Accel ()” and “Boost ()”, see code below. The problem is that they both pretty much do the same thing, except that “Boost” uses a different input and accelerates faster. The way I currently have it setup, the “Boost” function cancels out the “Accel” function, and I can only use boost…if I comment out the Boost function call in FixedUpdate, then the Accel function works again.

function FixedUpdate () {

	// Changes the drag value relative to current magnitude / body mass.
	rigidbody.drag = rigidbody.velocity.magnitude / (rigidbody.mass * 2);
	// Debug.Log("rigidbody drag is:  " + rigidbody.drag);
	
	// Call the "HasCollision" function to check for wheel collision for both wheels...
	HasCollision (frontWheelCollider, rearWheelCollider);		
	
	// Call the "Brakes" function for braking...
	Brakes (frontWheelCollider, rearWheelCollider);
	
	// Call the "Accel" function for acceleration...
	Accel (rearWheelCollider);
	
	// Call the "Boost" function for boosting...
	Boost (rearWheelCollider);
}

Right now, the Boost function cancels out the Accel function…

Here are both functions below:

function Accel (rearCollider: WheelCollider) {
		
	var accel = Input.GetAxis ("Accel");
	
	
	if (useGearBox) {
		var relativeVelocity : Vector3 = transform.InverseTransformDirection(rigidbody.velocity);
		// iPhone input simulated with mouse click, works on iPhone touch screen...
		if (Input.GetMouseButton (0)) {
			controls.DetectInput ();
			// Debug.Log ("is accel on  : " + controls.accelOn);
			if (controls.accelOn == true) {	
					rearCollider.motorTorque = engineTorque * engineForceValues[currentGear];
				//Debug.Log ("rearCollider.motorTorque :  " + rearCollider.motorTorque);
			}
		}	
		else {
			rearCollider.motorTorque = 0; 	// Stop accel when button is unpressed...
		}

		UpdateGear(relativeVelocity);
	}
       controls.accelOn = false;
}
function Boost (rearCollider: WheelCollider) {

  var currentMagnitude	= rigidbody.velocity.magnitude;
  
  // If the boost button is pressed...
  if (Input.GetMouseButton (1)  boostIsOn) {
		// If rear wheel is on the ground...
		if (hasRearWC) {
			rearCollider.motorTorque	= motorPower * boostPower;	// Applies boost (motorPower * boostPower) to rear WheelCollider.
			rigidbody.AddRelativeForce (-Vector3.up * 1500);	// Applies downward force to the bike to keep it on the ground.
			LimitVelocity (false);	// Function which limits the bike's top speed while boosting.
			// Debug.Log("Boosting");
		}
		else {
			rearCollider.motorTorque	= motorPower * boostPower;	// Applies boost only (no downward force) when airborne.
		}
	}
	else {
		rearCollider.motorTorque		= 0;	// Set acceleration to null. Keeps the bike from accelerating on its own when the throttle button isn't being pressed anymore.
	}
  
}

I’m pretty sure I have to do some kind of “yield” or “return”, but I don’t know how and where…any help would be greatly appreciated :slight_smile:
Thanks,

Steph

Ultimately, both functions seems to have their effect by setting the value of rearCollider.motorTorque. Since Boost gets called after Accel, the motorTorque value set in Boost will be the value that gets used. You either need to call them conditionally in an “if” statement or arrange it so that Boost adds an extra value on top of Accel rather than just assigning a new value.

Andeeee, thanks for the help. I think adding an extra value on top of Accel is the way to go for my situation, seems to be working fine now :slight_smile: