Advertisement
Guest User

Crosshair and DoorOpening

a guest
Sep 26th, 2015
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.65 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CrosshairScript : MonoBehaviour
  5. {
  6.  
  7.     public Texture2D crosshairTexture;
  8.  
  9.     public int speed;
  10.     public float friction;
  11.     public float lerpSpeed;
  12.     public float xDeg;
  13.     public float yDeg;
  14.     public Quaternion fromRotation;
  15.     public Quaternion toRotation;
  16.  
  17.     void Start()
  18.     {
  19.         Cursor.visible = false;
  20.     }
  21.  
  22.     void Update()
  23.     {
  24.         Ray ray = Camera.main.ScreenPointToRay(new Vector2 (Screen.width/2, Screen.height/2));
  25.         RaycastHit hit;
  26.         if(Physics.Raycast(ray, out hit, 10))
  27.         {
  28.             if (hit.collider.tag == "Bookcase_door")
  29.             {
  30.                 if (Input.GetMouseButton(0))
  31.                 {
  32.                     if(Input.GetAxis("Mouse X") > 0)
  33.                     {
  34.                         Debug.Log(Input.GetAxis("Mouse X"));
  35.                         //xDeg -= Input.GetAxis("Mouse X") * speed * friction;
  36.                         yDeg += Input.GetAxis("Mouse Y") * speed * friction;
  37.                         fromRotation = hit.transform.rotation;
  38.                         toRotation = Quaternion.Euler(0, yDeg, 0);
  39.                         hit.transform.rotation = Quaternion.Lerp(fromRotation, toRotation, Time.deltaTime * lerpSpeed);
  40.                     }
  41.                     else if(Input.GetAxis("Mouse X") < 0)
  42.                     {
  43.                         Debug.Log(Input.GetAxis("Mouse X"));
  44.                         //xDeg -= Input.GetAxis("Mouse X") * speed * friction;
  45.                         yDeg -= Input.GetAxis("Mouse Y") * speed * friction;
  46.                         fromRotation = hit.transform.rotation;
  47.                         toRotation = Quaternion.Euler(0, yDeg, 0);
  48.                         hit.transform.rotation = Quaternion.Lerp(fromRotation, toRotation, Time.deltaTime * lerpSpeed);
  49.                     }
  50.  
  51.                 }
  52.             }
  53.             else if (hit.collider.tag == "Door")
  54.             {
  55.                 if (Input.GetMouseButton(0))
  56.                 {
  57.                     Debug.Log("Clicked Bookcase Door!");
  58.                     xDeg -= Input.GetAxis("Mouse X") * speed * friction;
  59.                     //yDeg += Input.GetAxis("Mouse Y") * speed * friction;
  60.                     fromRotation = hit.transform.rotation;
  61.                     toRotation = Quaternion.Euler(0, xDeg, 0);
  62.                     hit.transform.rotation = Quaternion.Lerp(fromRotation, toRotation, Time.deltaTime * lerpSpeed);
  63.                 }
  64.             }
  65.  
  66.         }
  67.     }
  68.  
  69.     void OnGUI()
  70.     {
  71.         GUI.DrawTexture(new Rect(Screen.width / 2-4, Screen.height / 2-4, 8, 8), crosshairTexture);
  72.     }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement