Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.04 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5. using Mono.Data.Sqlite;
  6. using System.Data;
  7. using System;
  8. using System.IO;
  9. using UnityEditor;
  10.  
  11.  
  12. public class GameCursor : MonoBehaviour
  13. {
  14.     public Camera viewCamera;
  15.     [SerializeField] private GameObject gameCursorPrefab;
  16.     [SerializeField] private GameObject teleportPrefab;
  17.     [SerializeField] private GameObject handPrefab;
  18.     [SerializeField] private Transform Player;
  19.     [SerializeField] private float RayLenght = 2f;
  20.  
  21.     private GameObject cursorInstance;
  22.     private GameObject teleportInstance;
  23.     private GameObject handInstance;
  24.  
  25.     // Use this for initialization
  26.     void Start()
  27.     {
  28.         cursorInstance = Instantiate(gameCursorPrefab);
  29.         teleportInstance = Instantiate(teleportPrefab);
  30.         handInstance = Instantiate(handPrefab);
  31.         cursorInstance.SetActive(true);
  32.         teleportInstance.SetActive(false);
  33.         handInstance.SetActive(false);
  34.     }
  35.  
  36.     // Update is called once per frame
  37.     void Update()
  38.     {
  39.         UpdateCursor();
  40.         CheckInput();
  41.  
  42.     }
  43.  
  44.     /// <summary>
  45.     /// Updates the cursor based on what the camera is pointed at.
  46.     /// </summary>
  47.     private void UpdateCursor()
  48.     {
  49.         // Create a gaze ray pointing forward from the camera
  50.         Ray ray = new Ray(viewCamera.transform.position, viewCamera.transform.rotation * Vector3.forward);
  51.         RaycastHit hit;
  52.         if (Physics.Raycast(ray, out hit, Mathf.Infinity)) {
  53.             if (hit.collider.tag == "Ground" && Physics.Raycast(ray, out hit, RayLenght)) {
  54.                 // If the ray hits something, set the position to the hit point and rotate based on the normal vector of the hit
  55.                 teleportInstance.SetActive(true);
  56.                 cursorInstance.SetActive(false);
  57.                 teleportInstance.transform.position = hit.point;
  58.                 teleportInstance.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);              
  59.             }
  60.             else {
  61.                 teleportInstance.SetActive(false);
  62.                 cursorInstance.SetActive(true);
  63.             }
  64.         }
  65.         else {
  66.             // If the ray doesn't hit anything, set the position to the maxCursorDistance and rotate to point away from the camera
  67.             cursorInstance.transform.position = ray.origin + ray.direction.normalized;
  68.             cursorInstance.transform.rotation = Quaternion.FromToRotation(Vector3.up, -ray.direction);
  69.         }
  70.  
  71.         if (Physics.Raycast(transform.position, transform.forward, out hit)) {
  72.             if (hit.collider != null && Physics.Raycast(ray, out hit, RayLenght - 0,5)) {
  73.                 handInstance.SetActive(true);
  74.                 cursorInstance.SetActive(false);
  75.                 var hitReceiver = hit.collider.gameObject.GetComponent<HitReceiver>();
  76.                 handInstance.transform.position = cursorInstance.transform.position;
  77.                 handInstance.transform.rotation = cursorInstance.transform.rotation;
  78.                 float y = handInstance.transform.eulerAngles.y;
  79.                 float z = handInstance.transform.eulerAngles.z;
  80.                 handInstance.transform.rotation = Quaternion.Euler(-30f,y,z);
  81.  
  82.                 if (hitReceiver != null) {
  83.                     hitReceiver.OnRayHit();
  84.                 }
  85.             } else
  86.             {
  87.                 handInstance.SetActive(false);
  88.                 cursorInstance.SetActive(true);
  89.             }
  90.         }
  91.     }
  92.  
  93.     private void CheckInput()
  94.     {
  95.         if (Input.GetButtonDown("Fire1")) {
  96.             // If it's not a double click, it's a single click.
  97.             // If anything has subscribed to OnClick call it.
  98.             Teleport();
  99.         }
  100.     }
  101.  
  102.     public void Teleport()
  103.     {
  104.         if (teleportInstance.activeInHierarchy) {
  105.             Vector3 markerPosition = teleportInstance.transform.position;
  106.             Player.position = new Vector3(markerPosition.x, Player.position.y, markerPosition.z);
  107.         }
  108.     }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement