Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class GroundEditor : MonoBehaviour {
- public Vector3[] vertices;
- public float speed;
- public float lastDistance = 99999999999999999999999999f;
- void Start () {
- vertices = GetComponent<MeshFilter> ().mesh.vertices;
- }
- void OnMouseDrag()
- {
- FindNearestPoint ();
- }
- void OnMouseUp()
- {
- lastDistance = 99999999999999999999999999f;
- }
- private void FindNearestPoint()
- {
- Ray inputRay = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit;
- int vertNumber =0;
- if (Physics.Raycast(inputRay, out hit)) {
- for(int i=0; i<vertices.Length; i++){
- float distance = GetDistance (hit.point, vertices [i]);
- if (distance < lastDistance) {
- lastDistance = distance;
- vertNumber = i;
- }
- }
- EditGround (vertNumber);
- }
- }
- private float GetDistance(Vector3 start, Vector3 end)
- {
- return (start - end).sqrMagnitude;
- //return Mathf.Sqrt (Mathf.Pow((start.x - end.x),2) + Mathf.Pow((start.y - end.y),2)+ Mathf.Pow((start.z - end.z),2));
- }
- private void EditGround(int number){
- vertices [number].z += speed;
- GetComponent<MeshFilter> ().mesh.vertices = vertices;
- GetComponent<MeshFilter> ().mesh.RecalculateBounds();
- GetComponent<MeshFilter> ().mesh.RecalculateNormals();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement