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 Xatoku · Mar 11, 2011 at 05:04 PM · followvalueywithout

Distance Between 2 Objects Without Y

I'll try to explain this as best I can. I have an enemy which will start running towards the Player if he's a certain distance away from it. This works fine, but the problem is that when the Player jumps, the Enemy thinks that the Player is far away because the Distance goes up because transform.position factors in the Y value. I basically need the enemy to calculate the distance between it and the Player without factoring in the Player's Y value in order to keep the enemy grounded. The way it works now, the enemy stops at the stop range, but if the player jumps he starts running underneath the player and into the sky.

Here's the distance calculation script:

var distanceToTarget = Vector3.Distance(transform.position, GameObject.FindWithTag("Player").transform.position);

Here's the following script:

function Patrol(){
    while(true){
        if( AttackRangeCheck() ){ // target object is within range
            if( !StopRangeCheck()){
                var newPos = Vector3(GameObject.FindWithTag("Player").transform.position.x, 0, GameObject.FindWithTag("Player").transform.position.z);
                transform.LookAt(newPos);
                transform.Translate(Vector3.forward*Time.deltaTime*moveSpeed);
                animation.CrossFade("run",0.2);
                isRunning = true;
            }
        }
        yield;
    }
}

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

3 Replies

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

Answer by Bunny83 · Mar 11, 2011 at 05:46 PM

Vector3.Distance just subtracts position two from position one and returns the vector length (magnitude).

var dist = Vector3.Distance(P1,P2);

would be the same as

var dist = (P2-P1).magnitude;

So you can calculate the vector yourself and before you get the magnitude just set y to 0:

var vectorToTarget = GameObject.FindWithTag("Player").transform.position - transform.position;
vectorToTarget.y = 0;
var distanceToTarget = vectorToTarget.magnitude;

ps. It doesn't matter whether you subtract P1 from P2 or P2 from P1, the resulting vector would just point in the opposite direction but the length (magnitude) will be the same.

Comment
Add comment · Show 1 · 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 Xatoku · Jun 27, 2011 at 04:24 PM 0
Share

Upping this because I've found that your distanceToTarget works much more reliably than the traditional version. Thanks a ton! You put an end to 2 days of work :P Great, now I've jinxed it.

avatar image
1

Answer by kromenak · Mar 11, 2011 at 05:37 PM

It would probably be easy to just nullify the data on the y-axis and then do a distance test, like so:

Vector3 my2dPos = transform.position;
my2dPos.y = 0;
Vector3 target2dPos = GameObject.FindWithTag("Player").transform.position;
target2dPos.y = 0;
var distanceToTarget = Vector3.Distance(my2dPos, target2dPos);

You could also use Vector2, but you'd have to be careful because that would get rid of the z-value as opposed to the y-value.

Then the only concern is when you have a situation where the two are very close to each other on the x-z plane, but are separated by a large height, like on the edge of a cliff. You'd probably want to have some y distance at which you no longer allow the enemy to follow the player, like a little less than the distance it would take to kill the enemy.

Comment
Add comment · Show 2 · 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 Bunny83 · Mar 11, 2011 at 05:47 PM 0
Share

You mixed C# with JS ;) He's using JS so you could change your answer to show at least one correct solution.

avatar image Bunny83 · Mar 11, 2011 at 05:50 PM 0
Share

Just before someone put me down, var would also be possible in C# but the question was obviously in JS. ;)

avatar image
0

Answer by NathanJSmith · Mar 22, 2019 at 07:23 AM

I'll keep it simple.

     public static float FlatDistance(Vector3 pos1, Vector3 pos2)
     {
         pos1.y = pos1.z;
         pos2.y = pos2.z;
         return Vector2.Distance(pos1, pos2);
     }
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Camera rotation around player while following. 6 Answers

Editing y value of model under a first person controller has no effect 1 Answer

Inverse the mouse position value 2 Answers

Adjusting ObjectLabel to activate animations 1 Answer

Camera focusing on an object specified in a script 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