Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using Pathfinding;
- public class BuildRoad : MonoBehaviour {
- public GameObject dirtRoad;
- public GameObject nodeControl;
- GameObject road;
- GameObject node;
- public LayerMask acceptableLayer;
- public bool buildingRoad;
- bool currentlyBuilding;
- Vector3 roadStartLocation;
- float roadLength;
- float gridSize;
- SaveManager save;
- void Start()
- {
- gridSize = 5f;
- save = GameObject.Find("CameraControl").GetComponent<SaveManager>();
- }
- void Update()
- {
- if(buildingRoad)
- {
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit = new RaycastHit();
- if(Physics.Raycast(ray, out hit, Mathf.Infinity, acceptableLayer))
- {
- int x = Mathf.FloorToInt(hit.point.x / gridSize);
- int z = Mathf.FloorToInt(hit.point.z / gridSize);
- node.transform.position = new Vector3(x * gridSize, hit.point.y, z * gridSize);
- }
- if(Input.GetMouseButtonDown(0))
- {
- currentlyBuilding = true;
- //ClickLocation(out roadStartLocation);
- road = Instantiate (dirtRoad) as GameObject;
- road.transform.position = node.transform.position + new Vector3(0, 0.1f, 0);
- }
- if(currentlyBuilding)
- {
- roadLength = updateRoadLive(road.transform.position, road);
- }
- if(Input.GetMouseButtonUp(0) && currentlyBuilding)
- {
- TerrainHelper.updateTextureWithinBounds(road);
- road.GetComponent<MeshRenderer>().enabled = false;
- BoxCollider boxCol = road.GetComponent<BoxCollider>();
- boxCol.size = new Vector3(boxCol.size.x, 1f, 1f);
- MyGUO guo = new MyGUO();
- guo.bounds = road.collider.bounds;
- guo.setPenalty = 0;
- guo.modifyTag = true;
- guo.setTag = 1;
- AstarPath.active.UpdateGraphs(guo);
- save.addObjectToGoList(road);
- road.GetComponent<RoadScript>().length = roadLength;
- road.GetComponent<RoadScript>().FindObstacles();
- currentlyBuilding = false;
- }
- if(Input.GetMouseButtonDown(1) && currentlyBuilding)
- {
- Destroy(road);
- currentlyBuilding = false;
- }
- if(Input.GetMouseButtonDown(1) && !currentlyBuilding)
- {
- Destroy(node);
- buildingRoad = false;
- }
- }
- }
- bool ClickLocation(out Vector3 point)
- {
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit = new RaycastHit();
- if(Physics.Raycast(ray, out hit, Mathf.Infinity, acceptableLayer))
- {
- point = hit.point;
- return true;
- }
- point = Vector3.zero;
- return false;
- }
- void OnTriggerEnter(Collider otherCollider)
- {
- }
- float updateRoadLive(Vector3 roadStart, GameObject roadTemp)
- {
- Vector3 roadEnd = roadStart;
- roadEnd = node.transform.position + new Vector3(0, 0.1f, 0);
- roadTemp.transform.rotation = Quaternion.FromToRotation(Vector3.right, roadEnd - roadStart);
- Vector3 rot = road.transform.eulerAngles;
- rot.y = Mathf.Round (rot.y / 90) * 90;
- roadTemp.transform.eulerAngles = rot;
- float length = Mathf.Clamp(Vector3.Distance(roadEnd, roadStart), 2.5f, Mathf.Infinity) + 2.5f;
- float width = 5;
- Vector3[] vertices = {
- new Vector3(-2.5f, 0, -width/2),
- new Vector3(length, 0, -width/2),
- new Vector3(length, 0, width/2),
- new Vector3(-2.5f, 0, width/2)
- };
- int[] triangles = {
- 1, 0, 2,
- 2, 0, 3
- };
- Vector2[] uv = {
- new Vector2(-2.5f, -width/2),
- new Vector2(length, -width/2),
- new Vector2(length, width/2),
- new Vector2(-2.5f, width/2)
- };
- Vector3[] normals = {
- Vector3.up,
- Vector3.up,
- Vector3.up,
- Vector3.up
- };
- Vector4[] tangent = {
- new Vector4(1f, 0f, 0f, 1f),
- new Vector4(1f, 0f, 0f, 1f),
- new Vector4(1f, 0f, 0f, 1f),
- new Vector4(1f, 0f, 0f, 1f)
- };
- Mesh mesh = new Mesh();
- mesh.vertices = vertices;
- mesh.triangles = triangles;
- mesh.uv = uv;
- mesh.normals = normals;
- //mesh.RecalculateNormals();
- mesh.tangents = tangent;
- mesh.name = "Road";
- MeshFilter mesh_filter = road.GetComponent<MeshFilter>();
- mesh_filter.mesh = mesh;
- BoxCollider boxCol = roadTemp.GetComponent<BoxCollider>();
- boxCol.size = new Vector3(length + 2.5f, 1f, 5f);
- boxCol.center = new Vector3((length - 2.5f) / 2 , 0f, 0f);
- return length;
- }
- public void startBuildingRoad()
- {
- buildingRoad = true;
- node = (GameObject)Instantiate (nodeControl, Vector3.zero, Quaternion.identity);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement