鼠标点击两点测距
鼠标在一个平面上点任意两点。然后测得两点间的距离(应该属于鼠标事件)哎。有悬赏都没有人写。自己解决把代码发上来。
using UnityEngine;
using System.Collections;
public class line3D : MonoBehaviour {
private LineRenderer lineRenderer;
private Vector3 position;
private Vector3 []dis = new Vector3;
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 = position;
if(LengthOfLineRenderer >= 2)
distance = distance + Vector3.Distance(dis,dis);
else
distance = 0;
}
}
if (Input.GetMouseButtonDown(2))
{
LengthOfLineRenderer = 0;
}
}
void OnGUI()
{
GUILayout.Label("当前距离:" + distance);
}
} 终于解决终于解决终于解决 ying2035 发表于 2013-10-14 09:37 static/image/common/back.gif
哎。有悬赏都没有人写。自己解决把代码发上来。
using UnityEngine;
终于解决终于解决终于解决终于解决 不错不错 LZ真是人才 很不错 好帖就是要顶 难得一见的好帖 LZ真是人才