• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by carking1996 · Aug 16, 2011 at 01:22 PM · ontriggerenterontriggerexit

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.

Comment
Add comment · Show 9
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kacer · Aug 16, 2011 at 01:30 PM 0
Share

please reformat your code, some of it is barely readably :)

avatar image carking1996 · Aug 16, 2011 at 01:34 PM 0
Share

What do you mean?

avatar image Kacer · Aug 16, 2011 at 01:37 PM 0
Share

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.

avatar image carking1996 · Aug 16, 2011 at 01:40 PM 0
Share

Well, seems as though I cannot fix it. :/ Also, answer the question? Please ignore the formatting. I'm new to unity answers. ;)

avatar image Kacer · Aug 16, 2011 at 01:48 PM 0
Share

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 :)

avatar image carking1996 · Aug 16, 2011 at 04:05 PM 0
Share

Yes. That's what makes it go towards the next waypoint. :)

avatar image aldonaletto · Aug 16, 2011 at 05:01 PM 0
Share

Try to transform your Update function in FixedUpdate (just add the prefix Fixed ) - it's a shoot in the dark, but many rigidbody problems arise from the different ti$$anonymous$$g of Update and FixedUpdate - you can zero the velocity in OnTriggerEnter (FixedUpdate cycle) and the next Update may alter it, for instance.

avatar image carking1996 · Aug 16, 2011 at 05:03 PM 0
Share

"..you can zero the velocity in OnTriggerEnter (FixedUpdate cycle) and the next Update may alter it, for instance."

What do you mean?

avatar image aldonaletto · Aug 16, 2011 at 11:17 PM 0
Share

As far as I know, collisions and trigger events are generated at the FixedUpdate rate, since they are physics stuff, while Update runs at its own pace (as fast as it can render frames). If you accelerate a rigidbody in Update and set its velocity to zero in OnTriggerEnter, Update can still accelerate it in the next frame unless you tell it to stop acceleration.
But I've been thinking about another thing in your case: should you break the wheels ins$$anonymous$$d of zero the rigidbody velocity? I know almost nothing about wheel colliders, so it may be a big rubbish, but I suspect the wheels drag the car's body using their torque, thus you must stop the car breaking the wheels. If this is nonsense, please forget about it - I'm totally ignorant about wheel colliders.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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).

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image carking1996 · Aug 18, 2011 at 11:55 AM 0
Share

I set the RigidBody to 100. Still doesn't completely stop. :( Also, I made the script into FixedUpdate. No difference.

avatar image roamcel · Aug 19, 2011 at 05:48 AM 0
Share

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.

avatar image
0

Answer by carking1996 · Aug 19, 2011 at 12:40 PM

The wheels have wheel colldiers, not rigidbodies...

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image roamcel · Aug 20, 2011 at 05:49 AM 0
Share

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).

avatar image carking1996 · Aug 20, 2011 at 12:22 PM 0
Share

The car itself has a rigidbody. the wheels and body are attached to it.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Weird OnTriggerEnter / Exit behavior 1 Answer

Why would OnTriggerEnter get called, but not OnTriggerExit? 0 Answers

Open and close doors 1 Answer

OnTriggerEnter change alpha to half over time. 3 Answers

How to use OnTriggerEnter with multiple triggered objects? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges