c# - 查找角色 Controller 下方的对象

标签 c# object scripting unity-game-engine

我有一个角色 Controller ,它沿着 z 轴以恒定速度移动。我想知道角色 Controller 下的楼层名称。角色 Controller 永远不会与地板碰撞。这是一个平行运动。我使用 Raycast 在 C# 中使用以下方法找到地板:

myray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(myray, myhit, 1000)) {
    Debug.DrawLine (ray.origin, hit.point);
    print(myhit.collider.name);
}

这会产生错误。有没有更好的解决方案?

最佳答案

很难判断您的问题是什么,但是将此行为附加到您的角色控制对象将获得光线转换击中的第一个对象的名称。

我使用反转图层蒙版来忽略“玩家”图层,我将角色控制对象设置到该图层。这样光线转换就不会击中地板之前的对象。

using UnityEngine;
using System.Collections;

public class GetFloorName : MonoBehaviour 
{
    public string NameOfRaycastHitObject;

    void Update () 
    {
        RaycastHit hitInfo;
        int layerMask = ~(1 << LayerMask.NameToLayer("Player"));
        float distance = 100f;

        if (Physics.Raycast(transform.position, Vector3.down, out hitInfo, distance, layerMask))
        {
            NameOfRaycastHitObject = hitInfo.collider.name;
        }
    }
}

关于c# - 查找角色 Controller 下方的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14826192/

相关文章:

c# - list<T> 字段的平均计数

c# - System.Speech.Recognition 选择识别配置文件

c# - 如何使用数据表绑定(bind)修改数据 GridView 列

javascript - 未捕获的类型错误 : Object #<object> has no method 'method'

debugging - 您使用/编写过哪些有用的 GDB 脚本?

c# - 服务器的图像文件路径不是 C : drive MVC5

Java 重写 Arrays.sort() 的compareTo 方法

ios - 可识别 CALayer 对象的 iOS 应用程序 UI 自动化工具

powershell - Powershell脚本无法将ForEach-Object识别为有效的cmdlet

bash - 有没有办法捕获未找到的 bash 用户命令并对其进行处理?