0

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;
        }
    }

}
  • 1
    Can you post your movement code? We need to know how you use the `Speed` variable. – Dávid Florek Jun 07 '17 at 11:07
  • Also where is the script attached? – Hristo Jun 07 '17 at 11:15
  • OnTriggerEnter is fired when an object enters a trigger in the object the script is attached to. What I mean is, your motorcycle has a trigger? maybe you should change the script so its attached to the speedfield instead – Daahrien Jun 07 '17 at 11:42
  • @Lestat I tried to attach a script to the speedfield, but still nothing happens: 'void OnTriggerEnter(Collider other) { if (other.gameObject.CompareTag ("Player")) { GetComponent ().Speed += 50; } }' – Tobias Lorenz Jun 07 '17 at 12:04

2 Answers2

1

Make sure that all items in this checklist are satisfied:

  • The motorcycle has a Rigidbody component.
  • The plane doesn't have a Rigidbody component (it's useless and it could slow things down).
  • The Mesh Collider of the plane has Convex and isTrigger set to true.
  • The OnTriggerEnter method is in the motorcycle script.
  • The thickness of the plane trigger is wide enough: if the motorcycle speed is too high, it can pass thru the plane without the physics engine being able to register the trigger.
  • If this is the case, try to set the 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.

Galandil
  • 4,169
  • 1
  • 13
  • 24
1

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; 
    }
}
Woltus
  • 438
  • 4
  • 14
  • Thanks, this is very helpful! But the effect holds on when I leave the speedfield. At the moment I leave the field, the speed should be normal. – Tobias Lorenz Jun 07 '17 at 15:15
  • Just add a OnTriggerExit method and change multiplier there ;) – Woltus Jun 07 '17 at 15:19
  • Somehow the speed value does'nt really affect the Motorcycle. In the inspector the speed multiplies as it should, but the motorcycle doesn't get faster. Seem's like this increases the number in the inspector but not really the speed. I'm searching for the mistake :) – Tobias Lorenz Jun 07 '17 at 15:48
  • Just had to do the whole thing with the EngineTorque instead of the Speed. However you were really helpful. Thanks a 1000 times :) – Tobias Lorenz Jun 07 '17 at 15:53