Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 A_C · Jun 17, 2012 at 05:59 AM · c#collisionairacing

How to have realistic collisions with AI in a racing game?

I am making a racing game in Unity. I started off following the Car Tutorial on the Unity website, and have added some basic AI using the SeekSteer script found here: [SeekSteer Script][1]http://www.unifycommunity.com/wiki/index.php?title=SeekSteer

The problem with that script is that the cars do not respond well to any sort of collision. Either they stay directly on their path or they move upwards off the ground, but continue on the path and slowly lower to the ground after some time.

I thought a good way to prevent this would be to momentarily disable the script upon collision for X amount of time, then reenable it shortly afterwards. However, I am having trouble doing this, and can't figure out the best way to do so.

Here is the collision code:

 void OnTriggerEnter(Collider other)
     {
         if(other.name == "Car")
         {
             collision = true;
             sleep();                
         }
     }
 
 IEnumerator sleep()
     {
         yield return new WaitForSeconds(sleepTime);
     }
 

And the update method:

 protected void Update()
     {
         if (!collision)
         {
             if(useRigidbody)
                 rigidmember.velocity = currentHeading * speed;
             else
                    xform.position +=currentHeading * Time.deltaTime * speed;
                 if(faceHeading)
                 xform.LookAt(xform.position+currentHeading);      
             if(Vector3.Distance(xform.position,waypoints[targetwaypoint].position)<=waypointRadius)
             {
                 targetwaypoint++;
                 speed = Random.Range(0.69F, 0.71F);
                 if(targetwaypoint>=waypoints.Length)
                 {
                     targetwaypoint = 0;
                     if(!loop)
                         enabled = false;
                 }     
             }
             
         }
         else if (collision)
         {
             sleep ();
             collision = false;
         }
 }

I can't figure out how to fix this problem.

Any help is greatly appreciated!!!

Comment
Add comment · Show 1
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 Fattie · Jun 17, 2012 at 12:46 PM 0
Share

basically, every single thing you re doing is wrong :--)

fortunately, modern video games have a whole "physics engine", you DO NOT yourself EVER, EVER, EVER have to do ANYTHING. You NEVER set the velocity, or calculate anything. You make REAL, ACTUAl objects, and apply forces, and the game engine does EVERYTHING else.

This must be the #1 beginner mistake in game engines lately !!

"How do I calculate a parabola..."

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Sapidus3 · Jun 17, 2012 at 07:13 AM

The problem is how your code is moving things.

 rigidmember.velocity = currentHeading * speed;//THIS IS NOT HOW TO DO IT

Is setting the velocity and ignoring any affect the acceleration of gravity might be having. Instead you will want to use AddForce. It's OK to set velocity directly when certain events happen, but setting it in update will cause it to basically ignore the physics.

Note if you keep adding force over and over again, you will start going very quick unless there is also drag or friction. The nice thing is that this can have your car slide to a nice stop if it turns off its motor.

Your other problem is that you are having it move towards the current heading no matter if it is on the ground or not. So even if it was miles up in the air, it would still be moving itself towards that point.

The easy way to do this is to perform a raycast towards the ground and see if it hits the ground.

So you could add something like:

 var hit : RaycastHit;
 if (Physics.Raycast (transform.position, -Vector3.up, hit, 100)) {
     var distanceToGround = hit.distance;
     if(distanceToGround > WHATEVER THE HEIGHT OF YOUR CAR IS){
          //All of your update code
     }
 }
 

So it performs a raycast downwards. If it doesn't hit anything or if the distance is too large, your code that makes the car "drive" is ignored.

Comment
Add comment · 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
0

Answer by reptilebeats · Jun 17, 2012 at 08:07 AM

Read up some tutorials on youtube this will explain a lot better,

Comment
Add comment · 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

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Movement around a huge object by avoiding obstacles 1 Answer

Distribute terrain in zones 3 Answers

Resize Array Based on Value 2 Answers

Multiple Cars not working 1 Answer

Collider Vision AI question. Solved! 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