Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 ourchat · Aug 04, 2011 at 04:03 AM · aicarracingrace

AI car Script problem

I have an problem whit A.I car.

How do I stop the car to drive backwards after a crash and return to its normal position and continue driving on your way?

here is the code:

  var FrontLeftWheel : WheelCollider; 
  var FrontRightWheel : WheelCollider;
 
  var EngineTorque : float = 600.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.5;//-1.5
 
 GetWaypoints(); }
 
 function FixedUpdate() {
 
 rigidbody.drag = rigidbody.velocity.magnitude / 250;
 
 NavigateTowardsWaypoint();
 
 FrontLeftWheel.motorTorque = EngineTorque * inputTorque;
 FrontRightWheel.motorTorque = EngineTorque * inputTorque;
 
 FrontLeftWheel.steerAngle = 20 * inputSteer; 
 FrontRightWheel.steerAngle = 20 * 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.5 ) //< 0.5 { inputTorque = RelativeWaypointPosition.z / RelativeWaypointPosition.magnitude - Mathf.Abs(inputSteer);//Abs
 
 } else { inputTorque = 0.0; }
 
 if ( RelativeWaypointPosition.magnitude < 15 ) //20 { currentWaypoint ++;
 
 if ( currentWaypoint >= waypoints.length ) { currentWaypoint = 0;
 
 }
 
 }
 
 }
Comment
Add comment · Show 2
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 JUnityer · Aug 04, 2011 at 05:33 AM 1
Share

Please format your code properly! Edit your post, select the whole script and press the 101010 button. It's much easier for people to help you if they can read your code :)

avatar image ourchat · Aug 04, 2011 at 03:15 PM 0
Share

Thank you, I myself was also confused by what I wrote yesterday :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by roamcel · Aug 04, 2011 at 08:58 AM

I'm sorry but I couldn't arse myself to decipher the code, but I have a simple suggestion:

Install a COLLIDER in your car (supposed that you haven't done it already), and in your vehicle script you implement the appropriate hit or trigger function, as detailed in the manual: http://unity3d.com/support/documentation/ScriptReference/Collider.html

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 ourchat · Aug 04, 2011 at 03:32 PM 0
Share

I have attached the collider in the car, but still, when a collision with another car and turned the waypoint position. The car was still running in fallback position

avatar image roamcel · Aug 04, 2011 at 08:20 PM 0
Share

If I'm not mistaken, what happens is that the collision changes the actual bearing of the car, so the FIRST direction calculations get offset. I think that after a collision you just need to make another call to NavigateTowardsWaypoint to assure that the correct new bearing is calculated.

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

AI Car Problem 1 Answer

Ai turning problem on Android - Question solved 0 Answers

How To Add AI To Car/Racer? 4 Answers

How to implement traffic system efficiently in Unity game to run smoothly on mobile device? 1 Answer

Car AI Avoiding Obstacle 0 Answers


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