目前我的代码如下所示:
Vector3 tempPos = transform.position;
Vector3 checkPos = new Vector3(tempPos.x, tempPos.y, Mathf.Round(tempPos.z + 1));
if (Input.GetKey(KeyCode.W))
{
Vector3 direction = checkPos - transform.position;
Ray ray = new Ray(transform.position, direction);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Debug.Log(hit.point);
}
Debug.DrawRay(transform.position, direction, Color.black, 20, false);
}
但即使我正对着一个对象,我的Debug.Log()语句也没有得到任何结果。事实上,我甚至无法调用Debug.DrawRay()语句,因为它也没有显示任何内容。我到底能做些什么呢?任何帮助,将不胜感激。提前致谢!
很难从小片段中了解问题,但您可以尝试:
Vector3 fwd = tempPos.transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(raycastObject.transform.position, fwd, out objectHit, 50))
{
Debug.Log("Hit");
}