Advertisement
VXP

Unity Doors v1

VXP
Sep 23rd, 2015
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.79 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class UseScript : MonoBehaviour {
  5.  
  6.     public GameObject pivotPoint;
  7.  
  8.     bool moveDirection = false; // false - close to open, true - open to close
  9.     bool doMove = false;
  10.     GameObject door = null;
  11.    
  12.     // Use this for initialization
  13.     void Start () {
  14.         Cursor.visible = false;
  15.         door = this.gameObject;
  16.     }
  17.    
  18.     // Update is called once per frame
  19.     void Update () {
  20.         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
  21.         RaycastHit hit;
  22.         GameObject objectHit = null;
  23.         if (Physics.Raycast(ray, out hit, 100.0f)) {
  24.         //    Debug.DrawLine (ray.origin, hit.point);
  25.             objectHit = hit.collider.gameObject;
  26.         }
  27.  
  28.         GameObject player = GameObject.Find("RigidBodyFPSController");
  29.        
  30.  
  31.         if (Input.GetKeyDown(KeyCode.E) && (objectHit != null))
  32.         {
  33.             float distance = Vector3.Distance(objectHit.transform.position, player.transform.position);
  34.             if ((objectHit == door) && (distance < 2)) // 1.32
  35.             {
  36.             //  moveDirection = !moveDirection;
  37.                 doMove = true;
  38.                 print("Pressed, distance: " + distance);
  39.             }
  40.         }
  41.     }
  42.  
  43.     void FixedUpdate()
  44.     {
  45.         if (doMove)
  46.         {
  47.             if (moveDirection == false) // close to open
  48.             {
  49.                 transform.RotateAround(pivotPoint.transform.position, Vector3.up, 50 * Time.deltaTime);
  50.                 if (transform.rotation.y > 0.5)
  51.                 {
  52.                     doMove = false;
  53.                     moveDirection = true; // next movedir = open to close
  54.                 }
  55.             }
  56.             else if (moveDirection == true) // open to close
  57.             {
  58.                 transform.RotateAround(pivotPoint.transform.position, -Vector3.up, 50 * Time.deltaTime);
  59.                 if (transform.rotation.y < 0.0)
  60.                 {
  61.                     doMove = false;
  62.                     moveDirection = false; // next movedir = close to open
  63.                 }
  64.             }
  65.         }
  66.     }
  67.  
  68.     void OnGUI()
  69.     {
  70.     //  Screen.lockCursor = true;
  71.         Cursor.lockState = CursorLockMode.Locked;
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement