Unity3d 如何使2D光线投射完全忽略具有特定标记的碰撞器
光线投射器将从枪中射出并击中枪本身。如果它没有击中自己,它会击中持枪的玩家。玩家是一个布娃娃,经常在枪前摔倒。我希望它射出,忽略自己和玩家,只探测风景。 我还对如何使用Debug.DrawRay(位置、方向、颜色)感到困惑;如何增加长度?debug.drawray是否没有长度选项? 我已经为此挣扎了几天了(我是一个初学者)。非常感谢您的帮助,非常感谢Unity3d 如何使2D光线投射完全忽略具有特定标记的碰撞器,unity3d,raycasting,Unity3d,Raycasting,光线投射器将从枪中射出并击中枪本身。如果它没有击中自己,它会击中持枪的玩家。玩家是一个布娃娃,经常在枪前摔倒。我希望它射出,忽略自己和玩家,只探测风景。 我还对如何使用Debug.DrawRay(位置、方向、颜色)感到困惑;如何增加长度?debug.drawray是否没有长度选项? 我已经为此挣扎了几天了(我是一个初学者)。非常感谢您的帮助,非常感谢 if (Input.GetKey(KeyCode.S)) { if (Input.G
if (Input.GetKey(KeyCode.S))
{
if (Input.GetKeyDown(KeyCode.D))
{
//run shoot function
StartCoroutine(Shoot());
}
}
}
IEnumerator Shoot()
{
//direction = destination - source
Vector2 direction = (ShootHere.transform.position - transform.position);
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, direction, 40);
Debug.DrawRay(transform.position, direction, Color.red);
//This is me trying to get the raycast to not hit the player hitbox, this does not do that, it just causes
//the raycast to end the detection when it hits the object and instead procead to draw a line (The else statement)
//How do I get my raycast to ignore the tagged objects and to detect the next object past itself and the player?
if (hitInfo && hitInfo.collider.tag != "Player1" && hitInfo.collider.name != gameObject.name)
{
Debug.Log("RayCast hit a hitbox, the name is" + hitInfo.collider.name);
lineRenderer.positionCount = 2;
lineRenderer.SetPosition(0, transform.position);
lineRenderer.SetPosition(1, hitInfo.point);
}
else
{
lineRenderer.positionCount = 2;
lineRenderer.SetPosition(0, transform.position);
lineRenderer.SetPosition(1, ShootHere.position + ShootHere.right * 100);
}
lineRenderer.enabled = true;
yield return new WaitForSeconds(0.02f);
lineRenderer.enabled = false;
}
}
}我会改为使用,并在所有光线投射命中中循环
在世界坐标系中从start
到start+dir
绘制一条线。我将使用它,并在所有光线点击中循环
在世界坐标中从start
到start+dir
绘制一条线