JeCodeLeSoir

Untitled

Jul 23rd, 2022
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.17 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. //InteractObject.cs
  6. public abstract class InteractObject : MonoBehaviour
  7. {
  8.     public abstract void OnPlayerInterect(GameObject player);
  9. }
  10.  
  11. //InteractDoor.cs
  12. public class InteractDoor : InteractObject
  13. {
  14.     bool Open;
  15.     public override void OnPlayerInterect(GameObject player)
  16.     {
  17.         this.Open = !this.Open;
  18.     }
  19. }
  20.  
  21. //PlayerInterect.cs
  22. public class PlayerInterect : MonoBehaviour
  23. {
  24.  
  25.     //Layer object detecte
  26.     [SerializeField] LayerMask layerMask;
  27.     [SerializeField] float InterectDistance = 1.2f;
  28.  
  29.     void Update()
  30.     {
  31.         if (Input.GetKeyDown(KeyCode.E)) {
  32.             RaycastHit hit;
  33.             if (Physics.Raycast(transform.position,
  34.                 transform.TransformDirection(Vector3.forward), out hit,
  35.             InterectDistance, layerMask))
  36.             {
  37.                 InteractObject interactObj =
  38.                     hit.transform.GetComponent<InteractObject>();
  39.                 if (interactObj)
  40.                 {
  41.                     interactObj.OnPlayerInterect(this.gameObject);
  42.                 }
  43.             }
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment