Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by PStringer · Sep 08, 2021 at 11:00 AM · aicarcar physicscars

Whats wrong with my car auto park AI scripts?,What am I missing from my car auto park code?

Hi all,

I'm trying to create a simple AI car script that follows waypoints until it needs to park. All that stuff is sorted, the trouble I'm having is getting the car into the space smoothly. I'm using wheel colliders to steer the vehicle. Here's what I have:


Components

CarComponent - has all the movement logic + additional waypoint stuff which isn't relevant here.

ParkingSpaceComponent - Takes over control over the vehicle was triggered. Checks if the car is within it's bounds and the direction/angle of the car.


Code Inside ParkingSpaceComponent I have this function that checks the bounds of the car within the space. This works ok. Note, it's adapted from - https://forum.unity.com/threads/how-to-park-my-car-completely.882703/ - thanks @neginfinity !

 private ParkingData CheckInPlace()
     {
         var carTransform = OccupyingCar.transform;
 
         var carVectorUp = transform.InverseTransformDirection(carTransform.up);
         var carVectorForward = transform.InverseTransformDirection(carTransform.forward);
         var carVectorRight = transform.InverseTransformDirection(carTransform.right);
         var carVectorPos = transform.InverseTransformPoint(carTransform.position);      
 
         float difference = Mathf.Acos(Vector3.Dot(carTransform.forward, transform.forward));
 
         var halfWidthVector = carVectorRight * (carWidth * 0.5f);
         var halfLengthVector = carVectorForward * (carLength * 0.5f);
 
         //Corners of the car.
         var a = carVectorPos + halfWidthVector + halfLengthVector;
         var b = carVectorPos - halfWidthVector + halfLengthVector;
         var c = carVectorPos + halfWidthVector - halfLengthVector;
         var d = carVectorPos - halfWidthVector - halfLengthVector;
 
         var aabbMin = Vector3.Min(Vector3.Min(a, b), Vector3.Min(c, d));
         var aabbMax = Vector3.Max(Vector3.Max(a, b), Vector3.Max(c, d));
 
         var withinMinWidth = (aabbMin.x > -parkingLotWidth * 0.5f);
         var withinMaxWidth = (aabbMax.x < parkingLotWidth * 0.5f);
         var withinMinLength = (aabbMin.z > -parkingLotLength * 0.5f);
         var withinMaxLength = (aabbMax.z < parkingLotLength * 0.5f);
 
         //Actual overlap test:
         bool withinSpaceBounds =
             withinMinWidth
             && withinMaxWidth
             && withinMinLength
             && withinMaxLength;
 
         bool mostlyInPlace = (withinMinWidth && withinMaxWidth) && (withinMaxLength || withinMinLength);
 
         return new ParkingData(withinSpaceBounds, withinMinWidth, withinMinLength, withinMaxWidth, withinMaxLength);
     }



Then to get the steering angle I have this (which I think is where a possible issue might be?) where Direction is just Forward/Reverse.

    private float GetDifference(Direction direction)
     {
         return (direction == Direction.Forward) ? Mathf.Acos(Vector3.Dot(OccupyingCar.transform.forward, transform.forward)) : Mathf.Acos(Vector3.Dot(-OccupyingCar.transform.forward, -transform.forward)) * Mathf.Rad2Deg;
     }

Direction is determined like this

  private Direction GetCorrectingDir(ParkingData parkingData)
     {
         var f = Vector3.Dot(OccupyingCar.transform.forward, transform.forward);
         var r = Vector3.Dot(OccupyingCar.transform.right, transform.right);
 
         var alignedBackward = (f < 0 && r < 0);
 
         // if f && r < 0 then it's aligned but facing backwards
         // if f || r < 0 then it's the total wrong direction
         //     in this case, use the difference to found how to correct.
         //
         // if is facing backward, adjust difference calulation to use -Vector.forward (0,0,-1) 
         if (alignedBackward)
         {
             if (parkingData.WithinMaxLength)
                 return Direction.Reverse;
             else if (parkingData.WithinMinLength)
                 return Direction.Forward;
         }
         else
         {
             if (parkingData.WithinMaxLength)
                 return Direction.Forward;
             else if (parkingData.WithinMinLength)
                 return Direction.Reverse;
         }
 
         return Direction.Forward;
     }

Finally, to get the actual steering angle for the wheels, I have this.

   private float GetCorrectingSteer(ParkingData parkingData, Direction correctingDir)
     {       
         float steering = 0;
 
         var diff = Mathf.Clamp(GetDifference(correctingDir), -OccupyingCar.carSetting.maxSteerAngle, OccupyingCar.carSetting.maxSteerAngle);
 
         if (correctingDir == Direction.Forward)
         {  
             if (!parkingData.WithinMaxWidth)
                 steering = -OccupyingCar.carSetting.maxSteerAngle;
             else if (!parkingData.WithinMinWidth)
                 steering = OccupyingCar.carSetting.maxSteerAngle;
             else
                 steering = diff;
         }
         else
         {
             if (!parkingData.WithinMaxWidth)
                 steering = OccupyingCar.carSetting.maxSteerAngle;
             else if (!parkingData.WithinMinWidth)
                 steering = -OccupyingCar.carSetting.maxSteerAngle;
             else
                 steering = diff;
         }
         
         return steering;
     }

The idea here is that depending on which side of the lot the car is breaching, we set the turn angle to make the car move into the box.

Realise this might not be very clear but at this point I don't know what is relevant/wrong so willing to post more info if required.

Thanks in advance

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

0 Replies

· Add your reply
  • Sort: 

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

246 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

WheelCollider RPM values all over the place, how can I get more even values? 0 Answers

wheel collider / script offsets the meshes 0 Answers

How to stop the Unity Skycar Car without ending up in reversing? 0 Answers

What joint should I use for a trailer parking 2D game? 0 Answers

How to make a navmesh agent ignore navmesh borders 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