Advertisement
Ronin_de_Vreeze

Routesetting Backup #3

Apr 8th, 2020
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.14 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class Place : MonoBehaviour
  7. {
  8.     [Header("Hold settings")]
  9.     public List<GameObject> holds;
  10.     public GameObject selectedHold;
  11.     public bool holdSelected;
  12.     public int HoldsMask = 1024;
  13.     public int WallMask = 512;
  14.     public int WallAndHoldsMask;
  15.     public float holdUpRotation;
  16.  
  17.     [Header("UI settings")]
  18.     public Image cursor;
  19.  
  20.     [Header("Camera settings")]
  21.     public GameObject cam;
  22.  
  23.     [Header("Orbit settings")]
  24.     public float orbitSpeed;                                // The speed at which the camera lerps
  25.     public float orbitSensitivity;                          // The amount of orbiting for the mouse movement
  26.     private Vector3 orbitTarget;                            // The target of the camera rotation
  27.  
  28.     [Header("Zoom settings")]
  29.     public float zoomSpeed;                                 // The speed at which the zoom lerps
  30.     public float zoomSensitivity;                           // The amount of zoom for the mouse scolling
  31.     private Vector3 zoomTarget = new Vector3(0, 0, -10);    // The target of the zooming
  32.  
  33.     private void Start() {
  34.         WallAndHoldsMask = HoldsMask | WallMask;
  35.     }
  36.  
  37.     void Update()
  38.     {
  39.         if(holdSelected) {
  40.             // Move UI cursor
  41.             Vector2 pos = Camera.main.WorldToScreenPoint(selectedHold.transform.position);
  42.             cursor.gameObject.transform.position = pos;
  43.             Debug.DrawLine(selectedHold.transform.position, selectedHold.transform.position + selectedHold.transform.up, Color.red);
  44.         }
  45.  
  46.         if(Input.GetMouseButton(0) && holdSelected) {
  47.             // move selectedHold
  48.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  49.             RaycastHit hit;
  50.  
  51.             if (Physics.Raycast(ray, out hit, Mathf.Infinity, WallAndHoldsMask)) {
  52.                 selectedHold.transform.position = hit.point;                
  53.                 selectedHold.transform.up = hit.normal;
  54.                 Debug.Log("Is supposed to rotate " + holdUpRotation + " degrees.");
  55.                 selectedHold.transform.RotateAround(selectedHold.transform.position, selectedHold.transform.up, holdUpRotation);
  56.             }
  57.         }
  58.  
  59.         if(Input.GetMouseButtonDown(0) && !holdSelected) {
  60.             // Select raycasthit
  61.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  62.             RaycastHit hit;
  63.  
  64.             if (Physics.Raycast(ray, out hit, Mathf.Infinity, HoldsMask)) {
  65.                 cursor.enabled = true;
  66.                 selectedHold = hit.collider.gameObject;
  67.                 selectedHold.GetComponent<MeshCollider>().enabled = false;
  68.                 holdSelected = true;
  69.             }
  70.         }
  71.  
  72.         if(Input.GetMouseButtonDown(1) && !holdSelected) {
  73.             // Create new hold
  74.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  75.             RaycastHit hit;
  76.  
  77.             if (Physics.Raycast(ray, out hit, Mathf.Infinity, WallAndHoldsMask)) {
  78.                 selectedHold = Instantiate(holds[Random.Range(0, holds.Count)], hit.point, Quaternion.identity);
  79.                 holdSelected = true;
  80.                 cursor.enabled = true;
  81.                 selectedHold.transform.up = hit.normal;
  82.             }
  83.         }
  84.  
  85.         if(Input.GetMouseButton(1) && holdSelected) {
  86.             // Rotate selectedHold
  87.             float angle = Input.GetAxis("Mouse X") * 10;
  88.             selectedHold.transform.Rotate(Vector3.up, angle);
  89.             holdUpRotation += angle;
  90.             Debug.DrawLine(selectedHold.transform.position, selectedHold.transform.position + selectedHold.transform.forward, Color.blue);
  91.         }
  92.  
  93.         if(Input.GetKeyDown(KeyCode.Return) && holdSelected) {
  94.             // Deselect selectedHold            
  95.             holdSelected = false;
  96.             selectedHold.GetComponent<MeshCollider>().enabled = true;
  97.             selectedHold = null;
  98.             cursor.enabled = false;
  99.         }
  100.  
  101.         if(Input.GetKeyDown(KeyCode.Delete) && holdSelected) {
  102.             // Delete selectedHold
  103.             Destroy(selectedHold);
  104.             holdSelected = false;
  105.             selectedHold = null;
  106.             cursor.enabled = false;
  107.         }
  108.  
  109.         {
  110.             // Camera movement
  111.             if(Input.GetMouseButton(2)) {
  112.                 // Orbit camera
  113.                 orbitTarget += new Vector3(-Input.GetAxis("Mouse Y") * orbitSensitivity, Input.GetAxis("Mouse X") * orbitSensitivity, 0);
  114.                 orbitTarget.z = 0;
  115.             }
  116.  
  117.             // Apply orbit
  118.             Quaternion q = Quaternion.Euler(orbitTarget.x, orbitTarget.y, 0);
  119.             transform.rotation = Quaternion.Lerp(transform.rotation, q, Time.deltaTime * orbitSpeed);
  120.  
  121.             // Zoom camera
  122.             zoomTarget += new Vector3(0, 0, Input.mouseScrollDelta.y * zoomSensitivity);
  123.  
  124.             // Apply zoom
  125.             if(cam.transform.localPosition != zoomTarget) {
  126.                 cam.transform.localPosition = Vector3.Lerp(cam.transform.localPosition, zoomTarget, Time.deltaTime * zoomSpeed);
  127.             }
  128.         }  
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement