Problems with OnTriggerEnter and OnTriggerExit

Hello, I’m working on an ai script for traffic vehicles. The problem is, whe it hits the trigger, it should stop. It does stop, but then it continues going, but very slowly, until about 10 seconds after it exited the trigger. When The trigger is off, the vehicle does not move/continue to go.

Code starts here:


var speed : float = 2;
var maxTorque : float = 110;
var speed2 : float = 10;
var FrontLeftWheel : WheelCollider;
var FrontRightWheel : WheelCollider;
var GearRatio : float[];
var CurrentGear : int = 0;
var EngineTorque : float = 250.0;
var MaxEngineRPM : float = 3000.0;
var MinEngineRPM : float = 1000.0;
private var EngineRPM : float = 0.0;
var waypointContainer : GameObject;
private var waypoints : Array;
private var currentWaypoint : int = 0;
private var inputSteer : float = 0.0;
private var inputTorque : float = 0.0;

	function Start () {
	rigidbody.centerOfMass.y = -1;
	GetWaypoints();
	FrontLeftWheel.motorTorque =  maxTorque;
	FrontRightWheel.motorTorque =  maxTorque;
}

function Update () {
	rigidbody.drag = rigidbody.velocity.magnitude / 85;
	var mph = Mathf.Round (rigidbody.velocity.magnitude * 2);
	
	NavigateTowardsWaypoint();
	
	EngineRPM = (FrontLeftWheel.rpm + FrontRightWheel.rpm)/2 * GearRatio[CurrentGear];

    audio.pitch = Mathf.Abs(EngineRPM / MaxEngineRPM) + 1.0 ;

	if ( audio.pitch > 2.0 ) {
		audio.pitch = 2.0;
	}

	FrontLeftWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * inputTorque;
	FrontRightWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * inputTorque;
		
	FrontLeftWheel.steerAngle = 48 * inputSteer;
	FrontRightWheel.steerAngle = 48 * inputSteer;
}

function GetWaypoints () {
	var potentialWaypoints : Array = waypointContainer.GetComponentsInChildren( Transform );
	waypoints = new Array();
	
	for ( var potentialWaypoint : Transform in potentialWaypoints ) {
		if ( potentialWaypoint != waypointContainer.transform ) {
			waypoints[ waypoints.length ] = potentialWaypoint;
		}
	}
}

function NavigateTowardsWaypoint () {
	var RelativeWaypointPosition : Vector3 = transform.InverseTransformPoint( Vector3( 
												waypoints[currentWaypoint].position.x, 
												transform.position.y, 
												waypoints[currentWaypoint].position.z ) );

    inputSteer = RelativeWaypointPosition.x / RelativeWaypointPosition.magnitude;
	
	if ( Mathf.Abs( inputSteer ) < 0.2 ) {
		inputTorque = RelativeWaypointPosition.z / RelativeWaypointPosition.magnitude - Mathf.Abs( inputSteer );
	}else{
		inputTorque = 0.0;
	}

	if ( RelativeWaypointPosition.magnitude < 10 ) {
		currentWaypoint ++;
		
		if ( currentWaypoint >= waypoints.length ) {
			currentWaypoint = 0;
		}
	}	
}

function OnTriggerEnter (other : Collider) {
    if (other.gameObject.tag == "Stop") {
        rigidbody.velocity = Vector3.zero;
	}

    if (other.gameObject.tag == "AICar") {
        rigidbody.velocity = Vector3.zero;
        NavigateTowardsWaypoint();
    }
}

function OnTriggerExit(other : Collider) {
    if (other.gameObject.tag == "Stop") {
	   NavigateTowardsWaypoint();
	}
	
	if (other.gameObject.tag == "AICar") {	
	    NavigateTowardsWaypoint();
	}
}

Code ends here.

Can anyone tell me why it doesn’t work correctly? Thanks for any reply.

This might be a ‘simple’ mass issue.

Many initial implementations of physics solutions don’t take into account the necessary weight of the rigidbodies.

You MUST assure that ALL the rigidbodies on the car (and eventually scene) have appropriate mass values. Each mass unit is in fact 1 kilogram so,

if your car body weighs 1 kilogram (default rigidbody value) and each wheel weighs 1 kilogram, what you’re experiencing becomes somewhat understandable.

Check out what happens if you set your wheels masses to 2.5 and your car body to 100 (the docs suggest to not separate weights by more than 100 units).

The wheels have wheel colldiers, not rigidbodies…