为什么在某些地方用鼠标在地形上画线时没有画出任何东西?

Why when drawing a line with the mouse on the terrain in some places it's not drawing anything?

我想做的是免费绘图,所以当我移动鼠标时,它会绘制一条 continuous/consecutive 直线和曲线。但是在地形上的某些地方它没有绘制,而且在高 lands/hills 的某些地方它也没有一直绘制。即使我移动鼠标很慢。

LineRenderer 组件和脚本附加到相机。不是主摄像头而是新摄像头。

我之前也尝试过:

if (Physics.Raycast(ray, out hit, 1000))

但同样的问题。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DrawLinesWithMouse : MonoBehaviour
{
    private List<Vector3> pointsList;

    // Use this for initialization
    void Start()
    {
        pointsList = new List<Vector3>();
    }

    // Update is called once per frame
    void Update()
    {
        RaycastHit hit;
        //if (Physics.Raycast(ray, out hit, 1000))
        if (Physics.Raycast(GetComponent<Camera>().ScreenPointToRay(Input.mousePosition),out hit))
        {
            Vector3 hitpoint = hit.point;
            pointsList.Add(hitpoint);

            if (pointsList.Count > 1)
                DrawLine(pointsList[pointsList.Count - 2], pointsList[pointsList.Count - 1], Color.red, 0.2f);
        }
    }

    void DrawLine(Vector3 start, Vector3 end, Color color, float duration = 0.2f)
    {
        GameObject myLine = new GameObject();
        myLine.transform.position = start;
        myLine.AddComponent<LineRenderer>();
        LineRenderer lr = myLine.GetComponent<LineRenderer>();
        lr.startColor = color;
        lr.startWidth = 3f;
        lr.endWidth = 3f;
        lr.SetPosition(0, start);
        lr.SetPosition(1, end);
        //GameObject.Destroy(myLine, duration);
    }
}

物理相关的代码一般来说应该在 FixedUpdate 而不是 Update 中。

我自己之前遇到过一个错误,如果我在 Update 而不是 FixedUpdate 中进行光线投射,它有时会在不移动光标的情况下错过目标。

因此尝试将代码移至 FixedUpdate。