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 /
  • Help Room /
avatar image
0
Question by Akkronym · Oct 09, 2015 at 10:14 PM · vector3reversefleeingnormalization

Normalized Vector reversing direction unexpectedly

I'm writing a script to have an enemy run from anything tagged as a player.

This is the function where it checks to see if any players are within its sight radius, and if so calculates the sum of the vectors to each of them, flips the x and z components so as to get the exact opposite direction and then normalizes the vector which is then returned and used to by a different function to move it in that direction.

90% of the time this works as desired, but the other 10% of the time, it will suddenly change directions and start moving back towards the player and once on the other side of them, begin fleeing normally again.

Through the use of log messages, I've been able to figure out that it happens around the time that either the "x" or "z" values start approaching +/- 1. It appears that they change direction when they reach this threshold, and I'm pretty certain that this happens as a result of the normalization process.

I'm pretty new to unity and vectors aren't really my strong suit so I'm not sure what to do with this information to fix it.

 public static Vector3 observeForFlee(Vector3 center, float radius, string refTag) {
 
         Vector3 detectTotal = Vector3.zero;    //zero out summation vector
         bool detected = false; //flag variable to allow different behavior when nothing detected
 
         Collider[] seenObjects = Physics.OverlapSphere(center, radius); //observe all game objects within radius
 
         //cycle through all seenObjects
         for (int i = 0; i < seenObjects.Length; i++) {
             if(seenObjects[i].gameObject.transform.tag == refTag){ //if tagged with specified name
                 Vector3 detectCurrent = seenObjects[i].gameObject.transform.position; //calculate direct vector
                 detectCurrent.y = 0f; //zero out the y component - unneeded for my scenario
                 detectTotal = detectTotal + detectCurrent; //add vector to any other vectors of detected objects
                 detected = true; //put flag up
             }
         }
         if (detected) {
             //behavior is to go in best opposite direction so I flip the x and z components and the normalize
             Vector3 fleeVector = new Vector3(detectTotal.x * -1, 0, detectTotal.z * -1).normalized;
             return fleeVector;
         }
         else {
             //sentinel value checked outside of function
             return Vector3.zero; 
         }
     }
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
1
Best Answer

Answer by Statement · Oct 10, 2015 at 12:08 AM

Well, what bugs me a little here is that you get all players location, based from world center, not enemy center. You don't have a reference point to where the enemy stands, except for when you gather which players are in the sphere. Your coordinate systems won't add up. I think it should be fixed if you change detectCurrent though:

 Vector3 detectCurrent = seenObjects[i].gameObject.transform.position - center;


And if you want an explanation why, read on. Otherwise, the above change should suffice.

Without the correction with - center, it would only work correctly if the enemy was standing at (0, 0, 0). Imagine if the enemy was standing at (100, 0, 100) and a player was standing next by it at (101, 0, 100) (1 unit to the right of the enemy).

 // Renaming expressions to make it clearer to follow:
 
 // Vector3 detectCurrentBroken = seenObjects[i].gameObject.transform.position;
 // Vector3 detectCurrentBroken = player.position;
 Vector3 detectCurrentBroken = (101, 0, 100);
 
 // Vector3 detectCurrent = seenObjects[i].gameObject.transform.position - center;
 // Vector3 detectCurrent = player.position - center;
 // Vector3 detectCurrent = player.position - enemy.position;
 Vector3 detectCurrent = (101, 0, 100) - (100, 0, 100);
 Vector3 detectCurrent = (1, 0, 0);

Normalizing (101, 0, 100) would give you a "right + forward" direction, but the player was standing to the right of the enemy, not in front of the enemy.

Normalizing (1, 0, 0) would give you a "right" direction, which makes sense since the player was standing on the right side of the enemy.

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

32 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

Related Questions

how to calculate the angle between two vectors? 6 Answers

hi everyone how can i move an object from a point to specific point(point b) by mouse click 0 Answers

How do I get the world coordinates of the top left position of the camera? 3 Answers

Get angle of two Vector3s 1 Answer

Is there a way to Destroy a GameObject with given Coordinates? 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