Advertisement
EmmyDev

Player Interact

Dec 29th, 2022
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.40 KB | Source Code | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Interact : MonoBehaviour
  6. {
  7.     [SerializeField] private Camera playerCam;
  8.     void Update()
  9.     {
  10.         if(Input.GetKeyDown(KeyCode.E))
  11.         {
  12.             Ray ray = playerCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
  13.             RaycastHit hit;
  14.             if (Physics.Raycast(ray, out hit, 2f))
  15.             {
  16.                 if(hit.transform.GetComponent<TerminalBehaviours>() != null)
  17.                 {
  18.                    
  19.                     hit.transform.GetComponent<TerminalBehaviours>().InitInteraction(gameObject, playerCam, "E");
  20.                 }
  21.                 if(hit.transform.GetComponent<SecDoor>() != null)
  22.                 {
  23.                    
  24.                     hit.transform.GetComponent<SecDoor>().StartScan();
  25.                 }
  26.             }
  27.         }
  28.  
  29.         if(Input.GetKeyDown(KeyCode.Escape))
  30.         {
  31.             Ray ray = playerCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
  32.             RaycastHit hit;
  33.             if (Physics.Raycast(ray, out hit, 1f))
  34.             {
  35.                 if(hit.transform.GetComponent<TerminalBehaviours>() != null)
  36.                 {
  37.                    
  38.                     hit.transform.GetComponent<TerminalBehaviours>().InitInteraction(gameObject, playerCam, "Esc");
  39.                 }
  40.             }
  41.         }
  42.     }
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement