Blipples

PlayerInteractCode

May 9th, 2024
21
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | Source Code | 0 0
  1. public class PlayerController : NetworkBehaviour
  2. {
  3. public override void OnStartClient()
  4. {
  5. base.OnStartClient();
  6.  
  7. if (!base.IsOwner)
  8. {
  9. GetComponent<PlayerController>().enabled = false;
  10. }
  11.  
  12. ConfigureCharacter();
  13. }
  14.  
  15. // Update is called once per frame
  16. void Update()
  17. {
  18. // Interact with interactable object
  19. if (Input.GetKeyDown(KeyCode.E))
  20. {
  21. Interact();
  22. }
  23. }
  24.  
  25. private void Interact()
  26. {
  27. // If there's an interactable object, interact with it
  28. if (interactableObject == null)
  29. {
  30. LogManager.Instance.Log("There's nothing to interact with!");
  31. return;
  32. }
  33.  
  34. interactableObject.Interact(gameObject);
  35. }
  36. }
Advertisement
Comments
  • Blipples
    2 years
    # text 1.30 KB | 0 0
    1. private void OnTriggerEnter2D(Collider2D collision)
    2. {
    3. // Get x/y values for movement if the character isn't colliding with anything
    4. if (!base.IsOwner)
    5. {
    6. return;
    7. }
    8.  
    9. // Check if the collided object is interactable
    10. IInteractable interactableObjectColliding = collision.GetComponent<IInteractable>();
    11.  
    12. if (interactableObjectColliding != null)
    13. {
    14. interactableObjectName = collision.gameObject.name;
    15. interactableObject = collision.GetComponent<IInteractable>();
    16. LogManager.Instance.Log($"{gameObject.name} is near {interactableObjectName}");
    17. }
    18. }
    19.  
    20. private void OnTriggerExit2D(Collider2D collision)
    21. {
    22. // Get x/y values for movement if the character isn't colliding with anything
    23. if (!base.IsOwner)
    24. {
    25. return;
    26. }
    27.  
    28. // Check if the collided object is interactable
    29. IInteractable interactableObjectColliding = collision.GetComponent<IInteractable>();
    30.  
    31. if (interactableObjectColliding != null)
    32. {
    33. LogManager.Instance.Log($"{gameObject.name} is no longer near {interactableObjectName}");
    34. interactableObjectName = null;
    35. interactableObject = null;
    36. }
    37. }
Add Comment
Please, Sign In to add comment