|
楼主 |
发表于 2013-10-14 09:37
|
显示全部楼层
哎。有悬赏都没有人写。自己解决把代码发上来。
using UnityEngine;
using System.Collections;
public class line3D : MonoBehaviour {
private LineRenderer lineRenderer;
private Vector3 position;
private Vector3 []dis = new Vector3[1000];
private float distance = 0;
private int index = 0;
private int LengthOfLineRenderer=0;
public Camera cam;
RaycastHit hitt = new RaycastHit();
// Use this for initialization
void Start () {
lineRenderer = gameObject.AddComponent<LineRenderer>();
lineRenderer.material = new Material(Shader.Find("Particles/Additive"));
lineRenderer.SetColors(Color.yellow, Color.yellow);
lineRenderer.SetWidth(0.01f, 0.01f);
}
// Update is called once per frame
void Update () {
lineRenderer = GetComponent<LineRenderer>();
if (Input.GetMouseButtonDown(1))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray,out hit))
{
position = hit.point;
LengthOfLineRenderer++;
lineRenderer.SetVertexCount(LengthOfLineRenderer);
lineRenderer.SetPosition(LengthOfLineRenderer - 1, position);
dis[LengthOfLineRenderer - 1] = position;
if(LengthOfLineRenderer >= 2)
distance = distance + Vector3.Distance(dis[LengthOfLineRenderer - 2],dis[LengthOfLineRenderer - 1]);
else
distance = 0;
}
}
if (Input.GetMouseButtonDown(2))
{
LengthOfLineRenderer = 0;
}
}
void OnGUI()
{
GUILayout.Label("当前距离:" + distance);
}
} |
|