如何在x轴上找到两个游戏对象之间的距离? [英] How do I find the distance between two gameobjects just on the x axis?

查看:55
本文介绍了如何在x轴上找到两个游戏对象之间的距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为每个游戏对象制作了行,这些对象是我生下来的,如果它们在敌人的范围内,他们会攻击那个敌人然后继续前进。但是这些行很接近所以如果我对Gameobject的范围太大,那么一旦它们进入范围内,它们就会攻击它们旁边的行中的其他敌人。如果两个玩家在同一行,他们有相同的z位置,所以我想做的是,如果他们有相同的z位置,他们可以互相攻击,如果没有,那么他们就无法攻击。



我尝试过:



 < span class =code-keyword> float  distanceToEnemy = Vector3.Distance(transform.position,enemy.transform.position); 





这就是我试图获得的距离并且它有效,它只是我希望它更多的是盒子范围而不是球体范围。



<预LANG = C#> <跨度类= 代码关键字>如果(nearestEnemy = <跨度类= 代码关键字>空&安培;!&安培; shortestDistance <跨度类=code-keyword>< = range&& transform.position.z == nearestEnemy.transform.position.z){
target = nearestEnemy.transform;
inRange = true ;
} else {
target = null ;
inRange = false ;
}





这就是我所做的让玩家只射击与z具有相同z位置的敌人,但是它没有用,或者不能一直工作

解决方案

解决方案是第一个评论


I am made rows for each gameobject that I spawn in to go down and if they are within range of an enemy, they will attack that enemy and then keep going. But the rows are close so if I have the range of the Gameobject too big then they will attack the other enemies in the rows next to them once they come within range. If both players are in the same row they have the same z position and so what I want to do is, if they have the same z position, they can attack each other, if not, then they can't attack.

What I have tried:

float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position);



thats what I tried for getting the distance and it works, its just I want it to be more of a box range rather than a sphere range.

if (nearestEnemy != null && shortestDistance <= range && transform.position.z == nearestEnemy.transform.position.z) {
			target = nearestEnemy.transform;
			inRange = true;
		} else {
			target = null;
			inRange = false;
		}



and thats what I did to make the player shoot only the enemies that have the same z position as it, but it didn't work, or doesn't work all of the time

解决方案

the solution was the first comment


这篇关于如何在x轴上找到两个游戏对象之间的距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆