Reputation: 29
for a motorcycle racing game I want some parts of the track to increase the speed of the motorcycle.
I have a public float for the speed, which shows the speed in the inspector.
I added a plane with a Collider, which is a trigger and has the tag "speedfield" Now I thought, some simple code, attached to the motorcyclescript below, would do it:
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("speedfield")) {
Speed = Speed + 50;
}
}
But nothing happens. I think I'm missing something obvious here. Hope you can help! Greetings from germany :)
void FixedUpdate (){
Inputs();
Engine();
}
void Inputs (){
Speed = rigid.velocity.magnitude * 3.6f;
//Freezing rotation by Z axis.
transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, 0);
//If crashed...
if(!crashed){
if(!changingGear)
motorInput = Input.GetAxis("Vertical");
else
motorInput = Mathf.Clamp(Input.GetAxis("Vertical"), -1, 0);
steerInput = Input.GetAxis("Horizontal");
}else{
motorInput = 0;
steerInput = 0;
}
//Reverse bool
if(motorInput < 0 && transform.InverseTransformDirection(rigid.velocity).z < 0)
reversing = true;
else
reversing = false;
}
void Engine (){
//Steer Limit.
SteerAngle = Mathf.Lerp(defsteerAngle, highSpeedSteerAngle, (Speed / highSpeedSteerAngleAtSpeed));
FrontWheelCollider.steerAngle = SteerAngle * steerInput;
//Engine RPM.
EngineRPM = Mathf.Clamp((((Mathf.Abs((FrontWheelCollider.rpm + RearWheelCollider.rpm)) * gearShiftRate) + MinEngineRPM)) / (currentGear + 1), MinEngineRPM, MaxEngineRPM);
// Applying Motor Torque.
if(Speed > maxSpeed){
RearWheelCollider.motorTorque = 0;
}else if(!reversing && !changingGear){
RearWheelCollider.motorTorque = EngineTorque * Mathf.Clamp(motorInput, 0f, 1f) * engineTorqueCurve[currentGear].Evaluate(Speed);
}
if(reversing){
if(Speed < 10){
RearWheelCollider.motorTorque = (EngineTorque * motorInput) / 5f;
}else{
RearWheelCollider.motorTorque = 0;
}
}
}
Upvotes: 0
Views: 701
Reputation: 448
You overriding speed variable:
void FixedUpdate (){
Inputs();
Engine();
}
void Inputs (){
Speed = rigid.velocity.magnitude * 3.6f;
...
}
change it to:
[SerializeField] float speedMultiplier = 3.6f;
[SerializeField] float speedMultiplierChange = 2;
void Inputs (){
Speed = rigid.velocity.magnitude * speedMultiplier;
...
}
and
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("speedfield")) {
speedMultiplier += speedMultiplierChange;
}
}
Upvotes: 1
Reputation: 4249
Make sure that all items in this checklist are satisfied:
Rigidbody
component.Rigidbody
component (it's useless and it could slow things down).Mesh Collider
of the plane has Convex
and isTrigger
set to true
.OnTriggerEnter
method is in the motorcycle script.Collision Detection
of the motorcycle Rigidbody
to Continuous
and/or decrease somewhat the Fixed Timestep
in the Time project settings.Most probably the problem is due to the last two bullet points.
Upvotes: 1