view Assets/Application/Scripts/Test/MeshTest.cs @ 13:e297afe0889d default tip

Add Prefab.
author Kazuma Takeda
date Tue, 07 Feb 2017 20:49:26 +0900
parents 3fefb9f9025d
children
line wrap: on
line source

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MeshTest : MonoBehaviour {

	private LineRenderer pointer;
	private Mesh targetMesh;
	private Vector3 max;

	// Use this for initialization
	void Start () {
		pointer = this.GetComponent<LineRenderer> ();
		pointer.numPositions = 2;
	}
	
	// Update is called once per frame
	void Update () {
		this.pointer.SetPosition (0, this.transform.position);
		this.pointer.SetPosition (1, this.transform.forward * 10);
		RaycastHit hit;
		if (Physics.Raycast (transform.position, this.transform.forward * 10, out hit)) {
			Mesh hitMesh = hit.transform.GetComponent<MeshFilter> ().mesh;
			if(targetMesh == null || targetMesh != hitMesh)
				setMesh (hitMesh);

			Vector3 localPoint = hit.transform.InverseTransformPoint (hit.point);
			this.pointer.SetPosition (1, hit.point);
		}
	}

	public void setMesh (Mesh m) {
		targetMesh  = m;
		float max_y = 0f;
		float max_x = 0f;
		foreach (Vector3 v in m.vertices) {

			if (v.x >= max_x)
				max_x = v.x;

			if (v.z >= max_y)
				max_y = v.z;
		}
		print ("max (" + max_x + ", " + max_y + ")");
		max = new Vector3 (max_x, max_y);
	}
}