Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How do i make it so my character/camera stops moving when UI is open?

Discussion in 'Scripting' started by JoeP531, Feb 18, 2021.

  1. JoeP531

    JoeP531

    Joined:
    Feb 18, 2021
    Posts:
    1
    So I know I need to add a bool but I'm not sure where and what I'd put into it. Also, this code is not mine.

    Thanks.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Movement2 : MonoBehaviour
    6. {
    7.     [SerializeField] CharacterController controller;
    8.     private Vector3 playerVelocity;
    9.     private bool groundedPlayer;
    10.     public float playerSpeed = 5.0f;
    11.     private float jumpHeight = 40f;
    12.     public float gravityValue = -9.81f;
    13.  
    14.     public Transform Cam;
    15.  
    16.     public KeyCode jump;
    17.     public KeyCode FlyUp;
    18.     public KeyCode FlyDown;
    19.     public bool ablefly;
    20.  
    21.     private float  FlyMax = 250f;
    22.  
    23.     public Quest quest;
    24.     public int SoulHP = 30;
    25.     public bool cantMove;
    26.  
    27.     private void Start()
    28.     {
    29.         // add character controller
    30.         // controller = gameObject.AddComponent<CharacterController>();
    31.        
    32.     }
    33.  
    34.    
    35.  
    36.     void Update()
    37.     {
    38.  
    39.  
    40.  
    41.         // check if the player is grounded and the vector3.y < 0, if one of this condition is true set back the playerVelocity.y = 0f;
    42.         groundedPlayer = controller.isGrounded;
    43.         if (groundedPlayer && playerVelocity.y < 0)
    44.         {
    45.             playerVelocity.y = 0f;
    46.         }
    47.  
    48.         Vector3 flyup = new Vector3(0, 50, 0);
    49.         Vector3 flydown = new Vector3(0, -50, 0);
    50.         Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    51.  
    52.         // Look for the camera y Rotation and multiply it for the vector 3 move in order make the player direction y as same as the camera.
    53.        Vector3 FollowCam = Quaternion.Euler(0, Cam.eulerAngles.y, 0) * move;
    54.  
    55.         controller.Move(FollowCam * Time.deltaTime * playerSpeed);
    56.  
    57.  
    58.  
    59.         // makes the player jump by adding a float value to the vector 3 y
    60.         if (Input.GetKeyDown(jump) && ablefly == false && groundedPlayer == true)
    61.         {
    62.             playerVelocity.y += 4f;
    63.             //print("Diocane");
    64.         }
    65.  
    66.         if (Input.GetKeyDown(FlyUp) && ablefly == true)
    67.         {
    68.            
    69.             //flyup = Vector3.ClampMagnitude(flyup, 250f);
    70.             gravityValue = 0;
    71.             controller.Move(flyup * Time.deltaTime * playerSpeed);
    72.            
    73.         }
    74.         if (cantMove)
    75.         {
    76.  
    77.         }
    78.  
    79.         if (Input.GetKeyDown(FlyDown) && ablefly == true)
    80.         {
    81.             //flyup = Vector3.ClampMagnitude(flyup, 250f);
    82.             gravityValue = 0;
    83.             controller.Move(flydown * Time.deltaTime * playerSpeed);
    84.            
    85.         }
    86.      
    87.         playerVelocity.y += gravityValue * Time.deltaTime;
    88.         controller.Move(playerVelocity * Time.deltaTime);
    89.  
    90.     }
    91.  
    92.     public void GoFind()
    93.     {
    94.         if (quest.isActive)
    95.         {
    96.             quest.goal.ItemCollected();
    97.             if (quest.goal.IsReached())
    98.                 SoulHP += 30;
    99.         }
    100.     }
    101. }
    102.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class QuestGiver : MonoBehaviour
    7. {
    8.     public Quest quest;
    9.  
    10.     public GameObject questWindow;
    11.     public Text titleText;
    12.     public Text descriptionText;
    13.  
    14.     public Movement2 player;
    15.    
    16.  
    17.      public void OpenQuestWindow()
    18.      {
    19.         questWindow.SetActive(true);
    20.         titleText.text = quest.title;
    21.         descriptionText.text = quest.description;
    22.        
    23.      }
    24.  
    25.     public void AcceptQuest()
    26.     {
    27.         questWindow.SetActive(false);
    28.         quest.isActive = true;
    29.         player.quest = quest;
    30.  
    31.        
    32.     }
    33.    
    34.  
    35.  
    36.     void Start()
    37.     {
    38.        
    39.         questWindow = GameObject.Find("QuestWindow");
    40.  
    41.         questWindow.SetActive(false);
    42.     }
    43. }
     
  2. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    348
    Make a CameraCanMove bool in the first script.

    Put the if statement around whatever moves the camera in the first script :) At a glance, 53 and 55?

    If the camera follows the player and this is the player move script, you might even be better off wrapping the entire movement inside your bool, otherwise trying to stop the camera but allowing the player to move might not help much.

    Looks like your QuestGiver class already has a reference to the Movement2 script on line 14.

    You can set the value of the bool through that. Probably just after you set the QuestWindow to be active. Toggle the bool to false anywhere the window is closed.
     
    Last edited: Feb 18, 2021