Advertisement
NeoGriever

Untitled

Dec 11th, 2020
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.68 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class DoorDrag : MonoBehaviour {
  6.     public GameObject playerCam;
  7.     public float maxDistance = 4.0f;
  8.     public string targetTag;
  9.     public Transform grabber; // here's the needed "grab"-object target
  10.  
  11.     private Ray playerAim;
  12.     private Vector3 currentTarget;
  13.     private GameObject currentHitElement;
  14.  
  15.     private void RaycastProc() {
  16.         playerAim = playerCam.GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
  17.         RaycastHit hit;
  18.         currentHitElement = null;
  19.  
  20.         Debug.DrawRay(playerAim.origin, playerAim.direction * maxDistance, Color.red, 0f, false);
  21.         currentTarget = playerAim.origin + playerAim.direction * maxDistance; // endpoint of ray
  22.  
  23.         if (Physics.Raycast (playerAim, out hit, maxDistance)){
  24.             if(hit.collider.gameObject.tag == targetTag) {
  25.                 currentTarget = hit.point; // hitpoint of ray, if found
  26.                 currentHitElement = hit.collider.gameObject;
  27.                 Debug.DrawRay(hit.point + new Vector3(-.3f, 0f, 0f), new Vector3(.6f, 0f, 0f), Color.green);
  28.                 Debug.DrawRay(hit.point + new Vector3(0f, -.3f, 0f), new Vector3(0f, .6f, 0f), Color.green);
  29.                 Debug.DrawRay(hit.point + new Vector3(0f, 0f, -.3f), new Vector3(0f, 0f, .6f), Color.green);
  30.             }
  31.         }
  32.     }
  33.  
  34.     void LateUpdate() {
  35.         RaycastProc();
  36.         grabber.position = currentTarget;
  37.     }
  38.  
  39.     void Update () {
  40.         SpringJoint j = grabber.gameObject.GetComponent<SpringJoint>();
  41.         if((Input.GetKeyDown(KeyCode.E) || Input.GetMouseButtonDown(0))) {
  42.             if(currentHitElement != null) {
  43.                 if(j.connectedBody == null) {
  44.                     // connect to door
  45.                 } else {
  46.                     // free door
  47.                 }
  48.             } else {
  49.                 // free door too
  50.             }
  51.         }
  52.     }
  53.  
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement