Tobias Lorenz
Tobias Lorenz

Reputation: 29

acceleration-field for unity3D racing game, c#

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

Answers (2)

Woltus
Woltus

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

Galandil
Galandil

Reputation: 4249

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.

Upvotes: 1

Related Questions