Advertisement
Andrew2002

Untitled

Feb 24th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class GroundEditor : MonoBehaviour {
  6.     public Vector3[] vertices;
  7.     public float speed;
  8.     public float lastDistance = 99999999999999999999999999f;
  9.     void Start () {
  10.         vertices = GetComponent<MeshFilter> ().mesh.vertices;
  11.     }
  12.  
  13.     void OnMouseDrag()
  14.     {
  15.         FindNearestPoint ();
  16.     }
  17.  
  18.     void OnMouseUp()
  19.     {
  20.         lastDistance = 99999999999999999999999999f;
  21.     }
  22.  
  23.     private void FindNearestPoint()
  24.     {
  25.         Ray inputRay = Camera.main.ScreenPointToRay(Input.mousePosition);
  26.         RaycastHit hit;
  27.  
  28.         int vertNumber =0;
  29.         if (Physics.Raycast(inputRay, out hit)) {
  30.             for(int i=0; i<vertices.Length; i++){
  31.                 float distance = GetDistance (hit.point, vertices [i]);
  32.                 if (distance < lastDistance) {
  33.                     lastDistance = distance;
  34.                     vertNumber = i;
  35.                 }
  36.             }
  37.             EditGround (vertNumber);
  38.         }
  39.     }
  40.  
  41.     private float GetDistance(Vector3 start, Vector3 end)
  42.     {
  43.         return (start - end).sqrMagnitude;
  44.         //return Mathf.Sqrt (Mathf.Pow((start.x - end.x),2) + Mathf.Pow((start.y - end.y),2)+ Mathf.Pow((start.z - end.z),2));
  45.     }
  46.  
  47.     private void EditGround(int number){
  48.         vertices [number].z += speed;
  49.         GetComponent<MeshFilter> ().mesh.vertices = vertices;
  50.         GetComponent<MeshFilter> ().mesh.RecalculateBounds();
  51.         GetComponent<MeshFilter> ().mesh.RecalculateNormals();
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement