简体   繁体   English

RaycastHit.point始终返回(0,0,0)

[英]RaycastHit.point always returns (0,0,0)

I am just getting started with unity, and I tried following along with a couple of tutorials. 我刚刚开始团结,我尝试了一些教程。 That all went well, and I was learning fine, until I tried to learn a little about navmesh agents. 一切都进行得很好,而且我学习得很好,直到尝试学习有关navmesh代理的知识。 I watched Brackeys' video on it and copied his code exactly, but it doesn't work for me. 我在上面观看了Brackeys的视频,并准确地复制了他的代码,但这对我不起作用。 After debugging, I figured out that the hit.point always returns (0,0,0) Can someone help me with this? 调试后,我发现hit.point总是返回(0,0,0)有人可以帮我吗?

using UnityEngine;
using UnityEngine.AI;

public class PlayerController : MonoBehaviour {

    public Camera cam;

    public NavMeshAgent agent;

    void Update () 
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = cam.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
                agent.SetDestination(hit.point);
            }
        }
    }
}

I'm answering it a little late, but maybe other people will benefit. 我要晚一点回答,但也许其他人会受益。
If your hit.point is (0, 0, 0), something close to it (0, 0, 0.0003) or very far away (0, 0, -5491), then it's possible that it didn't hit any collider at all. 如果您的hit.point是(0,0,0),接近(0,0,0.0003)或很远(0,0,-5491),那么它可能没有撞到任何对撞机所有。 Before using hit.point value you should always check if it hit something. 在使用hit.point值之前,应始终检查是否击中了东西。
Do so with if(hit.collider != null) . 使用if(hit.collider != null)

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

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