我有一个角色 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/