Advertisement
MariuszPoz

Unity - opening the door (modified)

Jan 4th, 2025 (edited)
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.33 KB | Source Code | 0 0
  1. // THIS SCRIPT IS EXPLAINED HERE:
  2. // https://unity-is-easy.blogspot.com/2025/01/interacting-with-objects-in-unity.html
  3. // VISIT MY PAGE TO LEARN SOME UNITY :)
  4.  
  5. using UnityEngine;
  6.  
  7. public class DoorController : MonoBehaviour
  8. {
  9.     public GameObject door;
  10.     public float openRot = 115; // angle when door is open
  11.     public float closeRot = 0; // angle when door is closed
  12.     public float speed = 2; // speed of opening/closing door
  13.     public bool opening; // true or false if open
  14.    
  15.     public Transform player; // Reference to the player (drag the player object here in the Inspector)
  16.     public Camera playerCamera; // Reference to the player's camera
  17.     public float activationDistance = 2f; // Distance threshold for interaction
  18.  
  19.  
  20.     // Update is called once per frame
  21.     void Update()
  22.     {
  23.         // Calculate distance between player and door
  24.         float distance = Vector3.Distance(player.position, transform.position);
  25.        
  26.         Vector3 currentRot = door.transform.localEulerAngles;
  27.         if (opening)
  28.         {
  29.             if (currentRot.y < openRot)
  30.             {
  31.                 door.transform.localEulerAngles = Vector3.Lerp(currentRot, new Vector3(currentRot.x, openRot, currentRot.z), speed * Time.deltaTime);
  32.             }
  33.         }
  34.         else
  35.         {
  36.             if (currentRot.y > closeRot)
  37.             {
  38.                 door.transform.localEulerAngles = Vector3.Lerp(currentRot, new Vector3(currentRot.x, closeRot, currentRot.z), speed * Time.deltaTime);
  39.             }
  40.         }
  41.  
  42.         // Check if player is close enough, looking at the door and presses the 'F' key
  43.         if (distance <= activationDistance && Input.GetKeyDown(KeyCode.F))
  44.         {
  45.             if (IsLookingAtDoor())
  46.             {
  47.                 ToggleDoor();
  48.                 Debug.Log("Opening the door!");
  49.             }
  50.            
  51.         }
  52.  
  53.  
  54.     }
  55.  
  56.     private bool IsLookingAtDoor()
  57.     {
  58.         Ray ray = new Ray(playerCamera.transform.position, playerCamera.transform.forward);
  59.        
  60.         if (Physics.Raycast(ray, out RaycastHit hit, activationDistance))
  61.         {
  62.             // Check if the raycast hits this door
  63.             return hit.collider.gameObject == gameObject;
  64.         }
  65.         return false;
  66.     }
  67.  
  68.     public void ToggleDoor()
  69.     {
  70.         opening = !opening;
  71.     }
  72. }
  73.  
Tags: C# Script Unity
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement