C# 移动鼠标时,如何在地形上绘制线? 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; 公共类抽绳室:MonoBehavior { 私有列表点列表; //用于初始化 void Start() { pointsList=新列表(); } //每帧调用一次更新 无效更新() { 雷卡斯特击中; Ray-Ray=GetComponent().ScreenPointToRay(输入.mousePosition); 如果(物理射线投射(射线,命中率,1000)) { 向量3生命点=生命点; pointsList.Add(生命点); 抽绳(pointsList[0],pointsList[pointsList.Count-1],Color.red,0.2f); } } 无效绘制线(矢量3开始、矢量3结束、颜色、浮动持续时间=0.2f) { GameObject myLine=新GameObject(); myLine.transform.position=开始; myLine.AddComponent(); LineRenderer lr=myLine.GetComponent(); lr.material=新材质(Shader.Find(“粒子/Alpha混合预乘”); lr.startColor=颜色; lr.startWidth=3f; 端面宽度=3f; lr.设置位置(0,启动); lr.设置位置(1,结束); //游戏对象。销毁(myLine,持续时间); } }

C# 移动鼠标时,如何在地形上绘制线? 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; 公共类抽绳室:MonoBehavior { 私有列表点列表; //用于初始化 void Start() { pointsList=新列表(); } //每帧调用一次更新 无效更新() { 雷卡斯特击中; Ray-Ray=GetComponent().ScreenPointToRay(输入.mousePosition); 如果(物理射线投射(射线,命中率,1000)) { 向量3生命点=生命点; pointsList.Add(生命点); 抽绳(pointsList[0],pointsList[pointsList.Count-1],Color.red,0.2f); } } 无效绘制线(矢量3开始、矢量3结束、颜色、浮动持续时间=0.2f) { GameObject myLine=新GameObject(); myLine.transform.position=开始; myLine.AddComponent(); LineRenderer lr=myLine.GetComponent(); lr.material=新材质(Shader.Find(“粒子/Alpha混合预乘”); lr.startColor=颜色; lr.startWidth=3f; 端面宽度=3f; lr.设置位置(0,启动); lr.设置位置(1,结束); //游戏对象。销毁(myLine,持续时间); } },c#,unity3d,unity5,C#,Unity3d,Unity5,这里的问题是,它像手持式折扇一样画了一条线: 但是我希望它根据鼠标的移动位置画一条直线,包括曲线,如果我在圆圈中移动鼠标,而不仅仅是直线 using System.Collections; using System.Collections.Generic; using UnityEngine; public class DrawLinesWithMouse : MonoBehaviour { private List<Vector3> pointsList; /

这里的问题是,它像手持式折扇一样画了一条线:

但是我希望它根据鼠标的移动位置画一条直线,包括曲线,如果我在圆圈中移动鼠标,而不仅仅是直线

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;

        Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit, 1000))
        {
            Vector3 hitpoint = hit.point;
            pointsList.Add(hitpoint);

            DrawLine(pointsList[0], 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.material = new Material(Shader.Find("Particles/Alpha Blended Premultiply"));
        lr.startColor = color;
        lr.startWidth = 3f;
        lr.endWidth = 3f;
        lr.SetPosition(0, start);
        lr.SetPosition(1, end);
        //GameObject.Destroy(myLine, duration);
    }
}
此零件从第一个点(poinstList[0])绘制到最后一个点(pointsList[pointsList.Count-1])。 它应该从第二个到最后一个点再到最后一个点

DrawLine(pointsList[0], pointsList[pointsList.Count -1], Color.red, 0.2f);

我建议你检查一下。你可以将一个空的游戏对象添加到你的鼠标位置,上面会有trailRenderer。你可能想从currentPt-1绘制到currentPt,而不是从pt0绘制到当前点
DrawLine(pointsList[pointsList.Count -2], pointsList[pointsList.Count -1], Color.red, 0.2f);