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 Cobradabest · Mar 17, 2013 at 07:23 PM · c#positionmathf

My AI keeps flickering between stopping and moving.

I'm having a problem with my Enemy AI, I have 2 animations set for my AI, an idle one and a running one, as you might of guessed, they're for standing and moving respectively.

However, the AI keeps switching between the 2 animations, regardless of whether or not they're actually moving. When debugging it, getting to log the bool that tells it if it's moving or not, I noticed it kept flickering between the true and false.

Here's the code, I hope you guys notice something I overlooked:

                 if (CompareVector3 (myTransform.position, previousPosition)) 
                 {
                     moving = false;
                 } 
                 else 
                 {
                     moving = true;
                 }
         public bool CompareVector3 (Vector3 a, Vector3 b)
         {
             if (Mathf.Approximately (a.x, b.x) &&
                 (Mathf.Approximately (a.y, b.y)) &&
                 (Mathf.Approximately (a.z, b.z))) {
                 return true;
             } 
 else 
 {
                 return false;
             }
         }

What am I doing wrong?

I know that isn't much info, but aside from the previousPosition value is set to the current position at the end of "Update()", that's the only code that relates to the problem.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sparkzbarca · Mar 17, 2013 at 09:36 PM

copy paste at the start of comparevector3

 Debug.log("a.x " + a.x + " b.x " + b.x + " are approx = " + Mathf.Approximately (a.x, b.x))
 Debug.log("a.y " + a.y + " b.y " + b.y + " are approx = " + Mathf.Approximately (a.y, b.y))
 Debug.log("a.z " + a.z + " b.z " + b.z + " are approx = " + Mathf.Approximately (a.z, b.z))

that will let you know if your results are making sense.

if you see really really small changes it's either wierd movement code or approx isn't doing a good job and you need to write up your own if approx statement

something like

if(mathf.abs(a.x - b.x) > .1)

Comment
Add comment · Show 5 · 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 Cobradabest · Mar 17, 2013 at 10:06 PM 0
Share

I've tried the "$$anonymous$$athf.abs" suggestion, and no matter how low, I go, it STILL flickers between true and false. (I've tried from 0.001 to 1), If I go too high, the animations stays at standing.

avatar image sparkzbarca · Mar 17, 2013 at 10:23 PM 0
Share

what did the debug show?

avatar image sparkzbarca · Mar 17, 2013 at 10:24 PM 0
Share

going low would increase the likelyhood of flikering not decrease it. basically > x where x is how much movement your willing to tolerate without considering it movement.

so > .1 is more likely to stop movement than

.000001

avatar image Cobradabest · Mar 17, 2013 at 10:27 PM 0
Share

I've just tried doing "transform.positon == previousposition", which means it needs to be exactly the same for it to be true, and it STILL flickers! I have a bigger problem than I thought!

avatar image sparkzbarca · Mar 17, 2013 at 10:40 PM 0
Share

well of course it would flicker small changes in position would make it so

Just post your log or i cant help you

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

12 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

Related Questions

Mathf.Lerp not working 2 Answers

need teleport script fixed slightly 1 Answer

C# Script Template - how to make custom changes? 1 Answer

C# sticking to Cube corners position (raycasting AI script) 1 Answer

Positioning a GameObject so I't wont be inside another object 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