移动鼠标时如何在地形上划线?

时间:2017-09-08 22:41:07

标签: c# unity3d unity5

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);
    }
}

这里的问题是,它正在绘制一条像手持式折扇一样的线:

handheld folding fan

但我希望它根据鼠标移动位置(包括曲线)绘制一条直线,例如我将鼠标移动到圆圈中,而不仅仅是直线。

1 个答案:

答案 0 :(得分:1)

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

这部分是从第一个点(poinstList [0])到最后一个点(pointsList [pointsList.Count -1])。 它应该从第二点到最后一点到最后一点。

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