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 DeadKenny · Sep 28, 2013 at 01:24 AM · raycastaipathfindingdirectioncar

AI create path, help.

Ok so I gave up the navmesh/agent way of doing AI for my vehicles. Its not going to work because of many reasons not just it being too stupid, so I am making my own. My game WIP: http://forum.unity3d.com/threads/200629-Omega-Void-WIP

What I need now is for my car to reorient itself left or right in the general direction of the target... Or turn around completely(but that is not needed yet).

I got it to stop and reverse so far but need it either to pick right or left depending on the direction of the target.

Here I am using 3 rays to detect whats in front and then stop. Shaped like spread out fork or three fingers pointing out at the front.

How would I make it choose to turn left(or right) and move forward until the rays are not hitting?

Code:

 target = waypointHolder.transform;
             
             Vector3 relativeWaypointPos = transform.InverseTransformPoint( new Vector3(wayPoints[currentWaypoint].position.x, transform.position.y,wayPoints[currentWaypoint].position.z));
     
             
             
     //        Vector3 targetDir = target.position - transform.position;
             
             targetDistance = Vector3.Distance(transform.position, target.transform.position);
             
             Vector3 chassisLook = chassis.TransformDirection(new Vector3(0, -navSeeDistance, 0));
             Vector3 left = chassis.TransformDirection(new Vector3(-navSeeLeftDist, -19, 0));
             Vector3 right = chassis.TransformDirection(new Vector3(navSeeRightDist, -19, 0));
             
            
             RaycastHit chit;
             
             
     
             Debug.DrawRay(chassis.position, chassisLook, Color.white);    
             Debug.DrawRay(chassis.position, left, Color.white);    
             Debug.DrawRay(chassis.position, right, Color.white);    
     
             //Raycasting
              if(Physics.Raycast(transform.position, chassisLook, out chit, navSeeDistance)){
                                     
     
                 
              if(chit.transform.gameObject.layer == LayerUnpassable01){
                     
                 
               pathBlocked = true;        
                     
               inputBrake = true;
               inputTorque = -100.0f;    
               
                     if(rigb.velocity.sqrMagnitude <= 20.0f){
                         
                         inputTorque =  0.0f;
                         
                     }
                 }
                 
             }else{
                     
     //            pathBlocked = false;
                 
         
             }
             
             
             
             //
         //This is where the car steers and AI inputs speed/torque in order to get to target.    
             if(pathBlocked == false){
             
             inputSteer = relativeWaypointPos.x / relativeWaypointPos.magnitude;
             
             
             if(Mathf.Abs(inputSteer)<0.5)
                 inputTorque = relativeWaypointPos.z / relativeWaypointPos.magnitude - Mathf.Abs(inputSteer);
             else
             inputTorque = 0.0f;
     
     
     
 
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 Fattie · Sep 28, 2013 at 10:02 AM 0
Share

suggest you just get the "Astar Project" from the asset store. Writing pathfinding or AI is simply a professional job, not a hobbyist job. Why waste months of your life? It makes no sense.

avatar image DeadKenny · Sep 28, 2013 at 12:11 PM 0
Share

Well ok fine then I'll give Rain indie a go.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RealSoftGames · Jul 19, 2015 at 02:20 PM

no don't listen, you can accomplish this, all that matters is what your end goal is, if you are a hobbyist, it is well worth learning, for your Car AI

https://www.youtube.com/watch?v=HeQ89e27ehc&list=PL67XFC3MYQ6JV0l9485jB1Dpn4atLsFSo

here is a link for a Car AI tutorial. you can probably find many more out their, the most common path is the raycasting which i believe you have managed to figure out already, i was doing a similar thing to this but i was doing it with a Drone, a little more tricky when this sucker doenst even sit on the ground.

if you are using the Navmesh agent or not, you can accomplish this using a Rigidbody

as you know you can obtain onHit with a raycast,

 Vector3 fwd = transform.TransformDirection(Vector3.forward);
         if (Physics.Raycast(transform.position, fwd, 10))
         {
            //apply Angular Velocity to rigidbody, or depending on how you control your car, ifd you use the wheel physics make the the approriate wheels turn or accelerate(if you are using Tank Mechanics)
         }

http://docs.unity3d.com/ScriptReference/Physics.Raycast.html

when the ray is not hitting anything it will continue driving forward, unless you have other behaviors that do other things.

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

16 People are following this question.

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

Related Questions

Car AI Avoiding Obstacle 0 Answers

Car Ai - Calculate max corner speed 1 Answer

AI question, characters escaping from light 0 Answers

Have falling object exit from a collider after collision? 2 Answers

AI Player Jerky Movement - Overcoming 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