- Home /
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.
please reformat your code, some of it is barely readably :)
not all the code you have written is being formatted as code, but as regular text, therefore some of the text is just being written as one long line, which is very very hard to read.
Well, seems as though I cannot fix it. :/ Also, answer the question? Please ignore the formatting. I'm new to unity answers. ;)
the "navigate towards waypoint" function, is that the part of the script thats responsible for the navigation, its, from what i can glance, the cause of the issue, im not used to script in javascipt though, im a c# guy :)
Answer by roamcel · Aug 17, 2011 at 05:55 AM
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).
I set the RigidBody to 100. Still doesn't completely stop. :( Also, I made the script into FixedUpdate. No difference.
Curious. Try this:
EASY FIX:
set all the wheels rigidbodies to 'IS $$anonymous$$INE$$anonymous$$ATIC' true
REAL FIX:
1 - go into play mode
2 - brake till full stop
3 - select one of the wheels and change its rigidbody's $$anonymous$$ASS in real time (observe if the jitter increases or decreases)
4 - increase the rigidbody's DRAG value
This should let you understand what's going on.
Answer by carking1996 · Aug 19, 2011 at 12:40 PM
The wheels have wheel colldiers, not rigidbodies...
Then do realize that in your code you use
rigidbody.velocity = Vector3.zero;
which is obviously meaningless if there's no rigidbody (albeit if there's no rigidbody anywhere should raise an error).
The car itself has a rigidbody. the wheels and body are attached to it.
Your answer
Follow this Question
Related Questions
Trigger area around player. 1 Answer
Get WHICH trigger for OnTriggerEnter? 1 Answer
Stop the Audio when Player Leaves the Trigger? 2 Answers
OnTriggerExit overwrites OnTriggerEnter 2 Answers
OnTriggerExit happens too soon! 1 Answer