移动鼠标时如何在地形上画一条线?
How can i draw a line on the terrain when moving the mouse around?
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);
}
}
这里的问题是画线像手持折扇:
但我希望它根据鼠标移动位置绘制一条线,包括曲线,例如,如果我将鼠标移动到圆圈,而不仅仅是直线。
DrawLine(pointsList[0], pointsList[pointsList.Count -1], Color.red, 0.2f);
这部分是从第一个点(pointstList[0])到最后一个点(pointsList[pointsList.Count -1])绘制的。
它应该改为从倒数第二个点绘制到最后一个点。
DrawLine(pointsList[pointsList.Count -2], pointsList[pointsList.Count -1], Color.red, 0.2f);
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);
}
}
这里的问题是画线像手持折扇:
但我希望它根据鼠标移动位置绘制一条线,包括曲线,例如,如果我将鼠标移动到圆圈,而不仅仅是直线。
DrawLine(pointsList[0], pointsList[pointsList.Count -1], Color.red, 0.2f);
这部分是从第一个点(pointstList[0])到最后一个点(pointsList[pointsList.Count -1])绘制的。 它应该改为从倒数第二个点绘制到最后一个点。
DrawLine(pointsList[pointsList.Count -2], pointsList[pointsList.Count -1], Color.red, 0.2f);