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 siddharth3322 · Sep 08, 2019 at 01:37 PM · physicsraycastaipathfindingai problems

AI Player Jerky Movement - Overcoming Obstacle

I was working in the 3D war game. So I have written code for the AI player for movement, rotation, overcoming obstacle and shooting.

When an AI player requires to overcome from the midway obstacle, it is doing continuous jerk in the movement. Jerks mean it is doing continuous left-right rotation and very quickly.

The following image represents the overall situation that currently exists: alt text

AI Player is moving continuously from one place to another and try to find a way from placed obstacles within the environment. So AI player always has one target to reach so when it detects any obstacle it just does some rotation and then after again started moving ahead with already decided target.

It does not have any problem with bypassing the obstacle but its movement is not smooth that is the main problem.

Here is the complete code that I am using currently:

  private void FixedUpdate()
     {
         if (StopMovement)
             return;
 
         myRigidbody.MovePosition(myRigidbody.position + transform.TransformDirection(aiMovement) * movementSpeed * Time.fixedDeltaTime);
 
         TakeTurn();
     }
 
     private void TakeTurn()
     {
         Ray straightRightRay = new Ray(transform.position + Vector3.up * 0.5f + transform.right * 2f, transform.forward);
         Ray straightLeftRay = new Ray(transform.position + Vector3.up * 0.5f - transform.right * 2f, transform.forward);
 
         Debug.DrawRay(straightRightRay.origin, straightRightRay.direction * detectDistance, Color.green);
         Debug.DrawRay(straightLeftRay.origin, straightLeftRay.direction * detectDistance, Color.green);
 
         RaycastHit hit;
 
         // If we detect an obstacle on our right side, swerve to the left
         if (Physics.Raycast(straightRightRay, out hit, detectDistance, obstaclesLayerMask))
         {
             // Rotate left to avoid obstacle
             Rotate(-1);
         }
         else if (Physics.Raycast(straightLeftRay, out hit, detectDistance, obstaclesLayerMask))
         {
             // Otherwise, if we detect an obstacle on our left side, swerve to the right
             // Rotate right to avoid obstacle
             Rotate(1);
         }
         else
         {
             // Rotate the car until it reaches the desired chase angle from either side of the player
             if (Vector3.Angle(transform.forward, targetPoint.position - transform.position) > chaseAngle)
             {
                 followAngle = ChaseAngle(transform.forward, targetPoint.position - transform.position, Vector3.up);
                 Rotate(followAngle);
             }
             else
             {
                 // Otherwise, stop rotating
                 Rotate(0);
             }
         }
     }
 
     // Rotates the car either left or right, and applies the relevant lean and drift effects
     public void Rotate(float rotateDirection)
     {
         // If the car is rotating either left or right, make it drift and lean in the direction its rotating
         if (rotateDirection != 0)
         {        
             // Rotate the car based on the control direction
             myRigidbody.rotation = myRigidbody.rotation * Quaternion.Euler(Vector3.up * rotateDirection * rotationSpeed);
             currentRotation += rotateDirection * rotationSpeed * Time.deltaTime;
 
             if (currentRotation > 360)
                 currentRotation -= 360;
         }
         else
         {
             // Otherwise, if we are no longer rotating, straighten up the car and front wheels
             // Return the base of the car to its 0 angle
             myRigidbody.rotation = myRigidbody.rotation * Quaternion.Euler(Vector3.up * rotateDirection * rotationSpeed);
         }
     }
 
     // Calculates the approach angle of an object towrads another object
     public float ChaseAngle(Vector3 forward, Vector3 targetDirection, Vector3 up)
     {
         // Calculate the approach angle
         approachAngle = Vector3.Dot(Vector3.Cross(up, forward), targetDirection);
 
         // If the angle is higher than 0, we approach from the left ( so we must rotate right )
         if (approachAngle > 0f)
         {
             return 1f;
         }
         else if (approachAngle < 0f)
         {
             //Otherwise, if the angle is lower than 0, we approach from the right ( so we must rotate left )
             return -1f;
         }
         else
         {
             // Otherwise, we are within the angle range so we don't need to rotate
             return 0f;
         }
     }

Please give me your side suggestion to solve this problem. If you want video recording for jerky player movement then let me know, I will record a short video for you.

ai-player-obstacle-detection.png (327.3 kB)
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

266 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 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

Car AI Avoiding Obstacle 0 Answers

AI create path, help. 1 Answer

help with AI avoidance script 0 Answers

Physics.Raycast AI problem 1 Answer

Physics.Raycast 2 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