- Home /
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?
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?
Quite - indeed really you should know if they are "on the same platform" in some way
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.
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
}
}
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
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
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
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
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
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..
Your answer
Follow this Question
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