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 /
avatar image
0
Question by Codelyy · Dec 18, 2015 at 12:32 AM · c#3denemyenemyaivector3.distance

Using Vector3.distance without the y axis

So I'm using Vector3.distance to work out whenever the player is within a certain range of the enemy so the enemy can start chasing them and it works the way i want it. But the problem is I have some enemy's on high platforms that the player has to jump onto and once the player is on the same platform, I then want the enemy to start chasing the player. The problem i have currently is if the player is in range no matter if the enemy is higher then the player or lower, the enemy will start to chase the player which again... is not what i want

So i was wondering if there is a way using Vector3.distance to make it so it doesn't use the y axis. Or if there is another way i have to do it?

Comment
Add comment · Show 3
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 ZefanS · Dec 18, 2015 at 02:17 AM 0
Share

If I understand correctly, you don't want the enemy to detect the player if their heights are too different. In that case, why not check that the enemy's and the player's y-positions are within a certain threshold after the script deter$$anonymous$$es they are in range?

avatar image Codelyy ZefanS · Dec 18, 2015 at 02:27 AM 0
Share

How would i do that?

avatar image Fattie ZefanS · Dec 18, 2015 at 03:07 AM 0
Share

Quite - indeed really you should know if they are "on the same platform" in some way

5 Replies

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

Answer by Fattie · Dec 18, 2015 at 03:06 AM

Apart from anything else, you'll surely want the "FLAT" distance ...

this is common, you have such an extension in every project...

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 public static class Extns
     {
     public static Vector2 xz(this Vector3 vv)
         {
         return new Vector2(vv.x, vv.z);
         }
     
     public static float FlatDistanceTo(this Vector3 from, Vector3 unto)
         {
         Vector2 a = from.xz();
         Vector2 b = unto.xz();
         return Vector2.Distance( a, b );
         }
     }

use like this ...

 float howFar = a.FlatDistanceTo( b );

or perhaps

 d = transform.position.FlatDistanceTo( enemyPosition );


It may help.

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 AntonPetrov · Aug 09, 2020 at 06:19 PM 0
Share

Sometimes called "Horizontal Distance".

avatar image
1

Answer by ZefanS · Dec 18, 2015 at 02:31 AM

Something like:

 If (Vector3.Distance(enemy.transform.position, player.transform.position) < chaseDistance)
 {
     if (Mathf.Abs(player.transform.position.y - enemy.transform.position.y) < heightThreshold)
     {
         //Chase the player
     }
 }
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 Codelyy · Dec 18, 2015 at 02:37 AM 0
Share

Thank you :)

avatar image
-1

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

I'll keep it simple.

 public static float DistanceXZ ( Vector3 lhs , Vector3 rhs )
     => Vector2.Distance( new Vector2{x=lhs.x,y=lhs.z} , new Vector2{x=rhs.x,y=rhs.z} );

Edit: fixed

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 michaelfelleisen · May 18, 2021 at 09:57 AM 1
Share

EDIT: fixed

this does not work!

example: v1 = (0,0,0) v2 = (0,0,1)

distance is 1 your function will return ~1,414

but you are close.

 pos1.y = 0;
 pos2.y = 0;

if you do it like that your approach is definately my favourite

avatar image andrew-lukasik · May 18, 2021 at 11:23 AM 0
Share

(it's fixed now)

avatar image
-2

Answer by Zoelovezle · Dec 18, 2015 at 01:07 PM

Well the best way to do this use CirlcecastAll for 2D and SphereCastAll for 3D Using them is the easiest & best Option for you Question
Hope you got this :)

 //If you want to do same with more than one enemy you need to use CirclecastAll
 
 public float range ; // basically this is the radius 
 public LayerMask enemyMask ;  // Set a layer to your enemys and then set the same layer to the enemyMask 
 
 //Other modifications as per ur need
 public Vector2 Direction ;
 public float distance ;
 private Raycast2D _raycast ;
 
 void Range()
 {
     //This will cast a circle around Player and detects if the enemy is within range(radius of circlecast)
      _raycast = Physics2D.Circlecast(transform.position , range ,Direction ,distance ,enemyMask );
 
     //Now you can just apply any thing to your enemy if it comes within range
     if(_raycast) 
     {
         //   TriggerTheEnemyToAttackPlayer
     }
     
 }

Read This for understand Concepts(They are quiet best option )

CircleCast - http://docs.unity3d.com/ScriptReference/Physics2D.CircleCastAll.html SphereCast - http://docs.unity3d.com/ScriptReference/Physics.SphereCastAll.html

@Codelyoko363

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 Fattie · Dec 18, 2015 at 01:29 PM 1
Share

Hi Zoe - don't forget that the 2D physics system in Unity is completely separate from the 3D system.

The 2D system cannot help in any way with a 3D game like the one in this question.

note that in 3D, SphereCast casts a "tunnel". It does not cast a sphere.

it's very likely CheckSphere is more what you would mean to use here

avatar image Zoelovezle Fattie · Dec 19, 2015 at 08:55 PM 0
Share

Oh i had no idea u are making 3d game , its fine :) U would have said in questions u want it in 3d space :P

avatar image
0

Answer by logicandchaos · May 18, 2021 at 05:05 PM

Don't use Vector3.Distance, just subtract x of one from x from the other..

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

42 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

Related Questions

Boss AI help 1 Answer

[I REALLY NEED HELP FAST]Help with enemy Shooting 1 Answer

2D AI, Aim at player - even when jumping? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 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