Advertisement
ensiferum888

Building roads unity

Jul 14th, 2013
1,408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.44 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using Pathfinding;
  4.  
  5. public class BuildRoad : MonoBehaviour {
  6.    
  7.     public GameObject dirtRoad;
  8.     public GameObject nodeControl;
  9.     GameObject road;
  10.     GameObject node;
  11.    
  12.     public LayerMask acceptableLayer;
  13.    
  14.     public bool buildingRoad;
  15.     bool currentlyBuilding;
  16.    
  17.     Vector3 roadStartLocation;
  18.     float roadLength;
  19.    
  20.     float gridSize;
  21.     SaveManager save;
  22.    
  23.     void Start()
  24.     {
  25.         gridSize = 5f; 
  26.         save = GameObject.Find("CameraControl").GetComponent<SaveManager>();
  27.     }
  28.    
  29.     void Update()
  30.     {
  31.        
  32.         if(buildingRoad)
  33.         {
  34.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  35.             RaycastHit hit = new RaycastHit();
  36.        
  37.             if(Physics.Raycast(ray, out hit, Mathf.Infinity, acceptableLayer))
  38.             {
  39.                 int x = Mathf.FloorToInt(hit.point.x / gridSize);
  40.                 int z = Mathf.FloorToInt(hit.point.z / gridSize);              
  41.                
  42.                 node.transform.position = new Vector3(x * gridSize, hit.point.y, z * gridSize);
  43.             }
  44.            
  45.             if(Input.GetMouseButtonDown(0))
  46.             {
  47.                 currentlyBuilding = true;
  48.                 //ClickLocation(out roadStartLocation);
  49.                 road = Instantiate (dirtRoad) as GameObject;   
  50.                 road.transform.position = node.transform.position + new Vector3(0, 0.1f, 0);
  51.             }
  52.            
  53.             if(currentlyBuilding)
  54.             {
  55.                 roadLength = updateRoadLive(road.transform.position, road);
  56.             }
  57.            
  58.             if(Input.GetMouseButtonUp(0) && currentlyBuilding)
  59.             {
  60.                
  61.                 TerrainHelper.updateTextureWithinBounds(road);
  62.                 road.GetComponent<MeshRenderer>().enabled = false;
  63.                 BoxCollider boxCol = road.GetComponent<BoxCollider>();
  64.                 boxCol.size = new Vector3(boxCol.size.x, 1f, 1f);
  65.                 MyGUO guo = new MyGUO();
  66.                 guo.bounds = road.collider.bounds;
  67.                 guo.setPenalty = 0;
  68.                 guo.modifyTag = true;
  69.                 guo.setTag = 1;
  70.                 AstarPath.active.UpdateGraphs(guo);
  71.                 save.addObjectToGoList(road);
  72.                 road.GetComponent<RoadScript>().length = roadLength;
  73.                 road.GetComponent<RoadScript>().FindObstacles();
  74.                
  75.                 currentlyBuilding = false; 
  76.             }
  77.            
  78.             if(Input.GetMouseButtonDown(1) && currentlyBuilding)
  79.             {
  80.                 Destroy(road);
  81.                 currentlyBuilding = false;
  82.             }
  83.             if(Input.GetMouseButtonDown(1) && !currentlyBuilding)
  84.             {
  85.                 Destroy(node);
  86.                 buildingRoad = false;
  87.             }
  88.         }
  89.        
  90.        
  91.     }
  92.    
  93.     bool ClickLocation(out Vector3 point)
  94.     {
  95.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  96.         RaycastHit hit = new RaycastHit();
  97.        
  98.         if(Physics.Raycast(ray, out hit, Mathf.Infinity, acceptableLayer))
  99.         {
  100.             point = hit.point;
  101.             return true;
  102.         }
  103.         point = Vector3.zero;
  104.         return false;
  105.     }
  106.    
  107.     void OnTriggerEnter(Collider otherCollider)
  108.     {
  109.        
  110.     }
  111.    
  112.     float updateRoadLive(Vector3 roadStart, GameObject roadTemp)
  113.     {  
  114.         Vector3 roadEnd = roadStart;
  115.        
  116.         roadEnd = node.transform.position + new Vector3(0, 0.1f, 0);
  117.         roadTemp.transform.rotation = Quaternion.FromToRotation(Vector3.right, roadEnd - roadStart);
  118.        
  119.         Vector3 rot = road.transform.eulerAngles;
  120.         rot.y = Mathf.Round (rot.y / 90) * 90;
  121.         roadTemp.transform.eulerAngles = rot;
  122.        
  123.         float length = Mathf.Clamp(Vector3.Distance(roadEnd, roadStart), 2.5f, Mathf.Infinity) + 2.5f;
  124.                
  125.         float width = 5;
  126.                
  127.         Vector3[] vertices = {
  128.             new Vector3(-2.5f,      0, -width/2),
  129.             new Vector3(length, 0, -width/2),
  130.             new Vector3(length, 0, width/2),
  131.             new Vector3(-2.5f,      0, width/2)
  132.         };
  133.        
  134.         int[] triangles = {
  135.             1, 0, 2,
  136.             2, 0, 3
  137.         };
  138.        
  139.         Vector2[] uv = {
  140.             new Vector2(-2.5f, -width/2),  
  141.             new Vector2(length, -width/2),
  142.             new Vector2(length, width/2),
  143.             new Vector2(-2.5f, width/2)
  144.         };
  145.        
  146.         Vector3[] normals = {
  147.             Vector3.up,
  148.             Vector3.up,
  149.             Vector3.up,
  150.             Vector3.up         
  151.         };
  152.        
  153.         Vector4[] tangent = {
  154.             new Vector4(1f, 0f, 0f, 1f),
  155.             new Vector4(1f, 0f, 0f, 1f),
  156.             new Vector4(1f, 0f, 0f, 1f),
  157.             new Vector4(1f, 0f, 0f, 1f)
  158.         };
  159.        
  160.         Mesh mesh = new Mesh();
  161.        
  162.         mesh.vertices = vertices;
  163.         mesh.triangles = triangles;
  164.         mesh.uv = uv;
  165.         mesh.normals = normals;
  166.         //mesh.RecalculateNormals();
  167.         mesh.tangents = tangent;
  168.         mesh.name = "Road";
  169.        
  170.         MeshFilter mesh_filter = road.GetComponent<MeshFilter>();
  171.         mesh_filter.mesh = mesh;
  172.        
  173.         BoxCollider boxCol = roadTemp.GetComponent<BoxCollider>();
  174.         boxCol.size = new Vector3(length + 2.5f, 1f, 5f);  
  175.         boxCol.center = new Vector3((length - 2.5f) / 2 , 0f, 0f);
  176.        
  177.         return length;
  178.     }
  179.    
  180.     public void startBuildingRoad()
  181.     {
  182.         buildingRoad = true;
  183.         node = (GameObject)Instantiate (nodeControl, Vector3.zero, Quaternion.identity);
  184.     }
  185.    
  186.    
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement