Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Question How do I make this player controller work properly

Discussion in 'Scripting' started by garrylindseybuis, Feb 9, 2024.

  1. garrylindseybuis

    garrylindseybuis

    Joined:
    Oct 31, 2020
    Posts:
    9
    The idea for this script is that when the player interacts with a door his controls will be paused and he will be teleported to the other side and he will be able to move once again.
    I am using coroutine so that he also isn't able to spam the door teleport which works but the player controller still moves.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using SUPERCharacter;
    5.  
    6.  
    7.  
    8.  
    9. public class Door : MonoBehaviour, IInteractable
    10. {
    11.  
    12.   public SUPERCharacterAIO playerScript;
    13.   public AudioSource[] BUZZ;
    14.  
    15. public Transform player, destination;
    16. public GameObject playerg;
    17. private bool isInteracting;
    18. public bool Interact()
    19. {
    20.  
    21.  
    22.     if (isInteracting) return false;
    23.     if (playerScript.enableMovementControl) return false;
    24.     isInteracting = true;
    25.     playerScript.enableMovementControl = true;
    26.     StartCoroutine(enterRoom(1));
    27.     return true;
    28.  
    29. }
    30.  
    31.  
    32.  
    33.  
    34.     private IEnumerator enterRoom(float delay)
    35.     {
    36.  
    37.     yield return new WaitForSeconds(delay);
    38.     playerg.SetActive(false);
    39.     player.position = destination.position;
    40.     playerg.SetActive(true);
    41.      isInteracting = false;
    42.     playerScript.enableMovementControl = false;
    43.     }
    44.  
    45.  
    46. }
    As you can see I am trying to call the bool from the player script which is the main problem for me.
    How do I fix this so that it works?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,649
    Perhaps use the same isInteracting boolean by your player?

    This would require the player to track which door they are possibly manipulating for the duration of the manipulation.

    A more robust way is to have the interacting interface able to return a boolean that might indicate, "yeah, I'm still being interacted, so don't let the player move yet"