简体   繁体   English

忽略某些游戏对象的光线投射

[英]Raycast that ignore certain Game Objects

PlayerController Class PlayerController

public LayerMask groundLayer;

    void LookRotation()
{
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hitInfo;
    if (Physics.Raycast(ray, out hitInfo, groundLayer))
    {
        destination = new Vector3(hitInfo.point.x, transform.position.y, hitInfo.point.z);
        //Look at mouse position
        transform.LookAt(destination);
    }
}

I am having an issue getting my Raycast to either ignore my player or only detect the ground. 我在让Raycast忽略播放器或仅检测地面时遇到问题。 In the actual unity inspector I have set the LayerMask to be my Ground Layer and with my current code it will only detect the ground if it is in the Ground Layer although the player will still interfere with the Raycast even though it is not in the Ground Layer . 在实际的统一检查器中,我已将LayerMask设置为我的Ground Layer并且使用我当前的代码,即使播放器即使不在Ground Layer也仍会干扰Raycast,但它只会检测地面是否在Ground Layer中。 Ground Layer I need the Raycast to completely ignore the player and even though this is achieved by adding the player to the Ignore Raycast layer, I can't do that because the player will need to be detected by different Raycasts. 我需要Raycast完全忽略播放器,尽管这是通过将播放器添加到“ Ignore Raycast层来实现的,但我不能这样做,因为需要通过不同的Raycasts检测播放器。 all of this code occurs in my PlayerController script as it is used to determine my players rotation, I am unsure if that may be the reason the player interferes with the Raycast. 所有这些代码都发生在我的PlayerController脚本中,因为它用于确定玩家的轮换,但我不确定这是否是玩家干扰Raycast的原因。 Cheers in advance! 提前加油!

(1) Are you using the layer MASK the correct way? (1)您是否使用正确的MASK层?

It's more like this ... 更像这样...

int layerMaskYourHero = 1 << LayerMask.NameToLayer("YourHero");

and then 接着

    RaycastHit2D hit = Physics2D.Raycast(
        laserEmittingPoint.position, Vector2.left,
        castLength,
        layerMaskYourHero);

(2) Say, you're using the physics matrix right ? (2)说,您使用的是物理矩阵,对吗?

在此处输入图片说明

go to Edit -> Project Settings -> Physics 转到编辑->项目设置->物理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM