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 TheDavil86 · Jul 23, 2012 at 07:15 PM · vector3distance

Problem With Vector3.Distance

Alright so I'm working on some AI for a zombie and this should be fairly simple. If the Zombie is within hit range it stops moving towards me and hits me. This accomplished by the attack() function. Problem is that for some reason even if neither of us is moving the distance calculation continues to go up even if we're getting closer. So here is the code and tell me if you can spot what exactly is going wrong here.

 function AttackPlayer () {
       var lastVisiblePlayerPosition = target.position;
       while (true) {
           if (CanSeeTarget ()) {
                 distance = Vector3.Distance(transform.position, target.position);
                 Debug.Log(distance);
           
                 // Target is dead - stop hunting
                 if (target == null)
                      return;
 
                 // Target is too far away - give up
                 if (distance > attackRange)
                      return;
  
                 lastVisiblePlayerPosition = target.position;
                 if (distance >= dontComeCloserRange) {
                      MoveTowards(lastVisiblePlayerPosition);
                 }
  
                 if (distance < HitRange) {
                      Attack();
                 }
           } 
           else {
                yield StartCoroutine("SearchPlayer",
                lastVisiblePlayerPosition);

                // Player not visible anymore - stop attacking
                if (!CanSeeTarget ())
                    return;
           }
  
           yield;
      }
 }

It's worth pointing out that I monitored the actual number given by the Vector3.Distance equation while also monitoring the real position of the target, i.e. the player, and the transform, i.e. the zombie. And again even though both were standing still the distance continued to rise until 30 which is what the variable attackrange is set to. After that it never resets either, the distance just continuously goes up.

Comment
Add comment · Show 5
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 TheDavil86 · Jul 29, 2012 at 04:06 AM 0
Share

No one has any ideas on this?

avatar image Seth-Bergman · Jul 29, 2012 at 04:26 AM 0
Share

distance is not a static var, is it?

you should make sure the Vector3s being used to calculate the distance aren't changing if you're not moving.. judging by this, they may be somehow

add some more debug.logs:

distance = Vector3.Distance(transform.position, target.position);

             Debug.Log(distance);

             Debug.Log(transform.position);

             Debug.Log(target.position);

may help see whats going wrong

avatar image TheDavil86 · Jul 29, 2012 at 11:13 AM 0
Share

I did that before already and even if the 2 vector 3's are the same the number bounces between 2 numbers. I'm thinking about writing my own distance function to see if it works.

avatar image Seth-Bergman · Jul 29, 2012 at 11:34 AM 0
Share

if that's true, you must be accessing distance somewhere else I guess.. Vector3.Distance should work fine here.. you didn't answer, is distance static? are there multiple zombies in your scene?

avatar image TheDavil86 · Jul 29, 2012 at 12:02 PM 0
Share

No I initially had distance declared within that function so it would have been the only one using it. I then changed it to a private var that could be seen by any function in the script for debugging purposes. I found a fix for the problem although I'm not sure how it works. Essentially I just removed the script from the model and put it back on and that somehow fixed the problem.

1 Reply

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

Answer by TheDavil86 · Jul 29, 2012 at 01:06 PM

Ok I figured out the answer to my question. I'm not really sure what the problem was but by deleting the prefab and re-creating it, it fixed the problem. So anyone else that may experience this problem just do the same.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Vector3.Distance not working properly? 3 Answers

Find a position by knowing the directionVector and the distance to this point 1 Answer

Move an object to a specific distance 0 Answers

Change the "y" of a variable 1 Answer

Vector3(x,y,variable)? 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