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 Dreamerking007 · Aug 15, 2020 at 06:41 AM · rotationai

my ai wiggles slightly

 using UnityEngine;
 
 public class NewBehaviourScript : MonoBehaviour
 {
     public Vector2 lastKnownLocation;
     public Vector2 lastSetLocation;
     public Vector2 currentTarget;
     public float desiredRotation = 0;
     public float curRotation = 0;
     public float amountToRotate = 0;
 
     private Vector3 EulerRotation;
 
     void Start()
     {
         lastKnownLocation = transform.position;
         currentTarget = new Vector2(Random.Range(-150f, 150f), Random.Range(-100f, 100f));
     }
 
     void Update()
     {
         if ((currentTarget - lastKnownLocation).sqrMagnitude <= 9)
         {
             currentTarget = new Vector2(Random.Range(-150f, 150f), Random.Range(-100f, 100f));
         }
 
         desiredRotation = Vector2.SignedAngle(currentTarget - lastKnownLocation, transform.right);
 
         amountToRotate = 70 * Time.deltaTime;
 
         if (Mathf.Abs(desiredRotation) <= amountToRotate)
         {
             curRotation = desiredRotation + curRotation;
         }
 
         else
         {
             curRotation -= Mathf.Sign(desiredRotation) * amountToRotate;
         }
 
         if (curRotation > 180)
         {
             curRotation -= 360;
         }
         else if (curRotation < -180)
         {
             curRotation += 360;
         }
 
         EulerRotation = Quaternion.Euler(new Vector3(0f, 0f, curRotation)) * Vector3.right;
 
         lastKnownLocation = transform.position + (EulerRotation * 25 * Time.deltaTime);
         
         transform.eulerAngles = new Vector3(0, 0, curRotation);
         transform.position = lastKnownLocation;
     }
 }

in my above code i made a simple test bed for my ai to try to figure out why it wiggles. in this test bed it doesn't wiggle quite so much as in my actual game but, it still wiggles slightly. can anyone provide reason as to why it wiggles, and maybe a possible solution.

Edit: I found out the reason why it wiggles more on my game than it does on my test bed. It's because most of this code is actually in fixed update on my game increasing the wiggle range from about 1.1 - 1.4 degrees positive and negative

Comment
Add comment · Show 4
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 Spip5 · Aug 15, 2020 at 01:56 PM 0
Share

I havn't tested your script, but I find this line odd :

          lastKnownLocation = transform.position + (EulerRotation * 25 * Time.deltaTime);
 

Why is there a rotation value in your position value? Since your calculate rotation in update and use it in your position, my guess is this is the source of the wiggle

avatar image Dreamerking007 · Aug 16, 2020 at 10:35 AM 0
Share

that rotation is rotating the amount i'm moving if it wasn't there the ai would forever drive straight the transform.eulerangles happens after i change the position because in my actual game there is this whole check for obstacles function that happens between them sorry if this was confusing

it's the same value as transform.right would be it's not the cause of the wiggle i actually just checked to confirm and yeah the wiggle is still present

avatar image Spip5 Dreamerking007 · Aug 16, 2020 at 10:55 AM 0
Share

I justed tested your script. With smaller values overall I have no wiggle at all (with larger values, it goes way to fast for me to see).

     void Start()
     {
         lastKnownLocation = transform.position;
         currentTarget = new Vector2(Random.Range(-2f, 2f), Random.Range(-3f, 3f));
     }
 
     void Update()
     {
         if ((currentTarget - lastKnownLocation).sqr$$anonymous$$agnitude <= 0.5F)
         {
             currentTarget = new Vector2(Random.Range(-2f, 2f), Random.Range(-3f, 3f));
         }
 
 [...]
 
         lastKnownLocation = transform.position + (EulerRotation * 2 * Time.deltaTime);
 

In your script if you reduce the speed do you still see the wiggle ?

avatar image Dreamerking007 · Aug 16, 2020 at 11:06 AM 0
Share

sorry i should have mentioned that in my game the pixels per unit is set to 1 and the camera is set to 114

but either way even if you reduce the speed the wiggle is still there it's only about plus or $$anonymous$$us 2 degrees so it's not much of a wiggle but it's enough that he's crashing in my game where he really shouldn't if you watch the vehicles desired rotation in the inspector you'll notice that when it finally reaches 0 the desired rotation starts jumping plus or $$anonymous$$us about 2 degrees

1 Reply

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

Answer by Dreamerking007 · Aug 16, 2020 at 11:29 AM

i discovered the problem. the problem was that in the first if statement it should have been curRotation - desiredRotation if i change this

          if (Mathf.Abs(desiredRotation) <= amountToRotate)
          {
              curRotation = desiredRotation + curRotation;
          }
  
          else
          {
              curRotation -= Mathf.Sign(desiredRotation) * amountToRotate;
          }

into this

 if (Mathf.Abs(desiredRotation) <= amountToRotate)
         {
             amountToRotate = Mathf.Abs(desiredRotation);
         }
 
         curRotation -= Mathf.Sign(desiredRotation) * amountToRotate;

the problem is fixed and the wiggle becomes almost non-existent

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

251 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

Related Questions

Enemy Rotation AI 0 Answers

Unity AI Obstacle Avoidance Help 3 Answers

2D AI: How to get the AI to aim ahead of its target? 3 Answers

Spinning 360 degrees and stopping (AI) 1 Answer

Slerp rotation on collision - C# 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