Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Smart_GamingYT · Dec 15, 2021 at 11:27 AM · scripting problemuimovementnoobreturn

How can I make the player stop moving when a UI dialogue box is active?

Ok so I am a beginner at c# and was wondering how to make it so that when a dialogue box appears on the screen, the player can not move at all. I am doing a 2D top-down RPG game, and I have a nice dialogue, and movement script. I would also like to mention that I made a pause menu and figured out how to stop the player from moving when the pause menu is up, but I cant do the same with this dialogue box.

Here's my dialogue script:

using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro;

public class Dialougue : MonoBehaviour { public TextMeshProUGUI textComponent; public string[] lines; public float textSpeed;

 private int index;


 // Start is called before the first frame update
 void Start()
 {
     textComponent.text = string.Empty;
     StartDialouge();
 }

 // Update is called once per frame
 void Update()
 {
     if (Input.GetMouseButtonDown(0))
     {
         if (textComponent.text == lines[index])
         {
             NextLine();
         }
         else
         {
             StopAllCoroutines();
             textComponent.text = lines[index];
         }
     }
 }

 void StartDialouge()
 {
     index = 0;
     StartCoroutine(TypeLine());
 }

 IEnumerator TypeLine()
 {

     foreach (char c in lines[index].ToCharArray())
     {
         textComponent.text += c;
         yield return new WaitForSeconds(textSpeed);
     }

 }

 void NextLine()
 {
     if (index < lines.Length - 1)
     {
         index++;
         textComponent.text = string.Empty;
         StartCoroutine(TypeLine());
     }
     else
     {
         gameObject.SetActive(false);
     }
 }

}

And here is my player movement script:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PlayerMovement : MonoBehaviour {

 public float moveSpeed = 5f;

 public Rigidbody2D rb;
 public Animator animator;

 Vector2 movement;

 public VectorValue startingPosition;

 void Start()
 {
     transform.position = startingPosition.initalValue;
 }


 void Update()
 {
     


     if (!PauseMenu.isPaused)
     {
         movement.x = Input.GetAxisRaw("Horizontal");
         movement.y = Input.GetAxisRaw("Vertical");

         animator.SetFloat("Horizontal", movement.x);
         animator.SetFloat("Vertical", movement.y);
         animator.SetFloat("Speed", movement.sqrMagnitude);
     }
     
 }

 void FixedUpdate()
 {
     rb.MovePosition(rb.position + movement.normalized * moveSpeed * Time.fixedDeltaTime);
 }

}

Please, any help will be appreciated. I feel like this has a simple solution that I just don't know.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by cukrhraje · Dec 15, 2021 at 08:15 PM

Basicaly you need to disable movement script. Place this code where you starting the dialogue

gameObject.Find("PLAYER GAME OBJECT NAME OR GAMEOBJECT WITH MOVEMENT SCRIPT ATTACHED TO").GetComponent (PlayerMovement). enabled = false;

And this where you end the dialogue

gameObject.Find("PLAYER GAME OBJECT NAME OR GAMEOBJECT WITH MOVEMENT SCRIPT ATTACHED TO").GetComponent (PlayerMovement). enabled = true;

Hope this will help

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Smart_GamingYT · Dec 16, 2021 at 12:24 AM 0
Share

When I plug that in I get these two errors:

Severity Code Description Project File Line Suppression State Error CS0176 Member 'GameObject.Find(string)' cannot be accessed with an instance reference; qualify it with a type name instead

Severity Code Description Project File Line Suppression State Error CS0119 'PlayerMovement' is a type, which is not valid in the given context

avatar image
0

Answer by Pathojen · Dec 16, 2021 at 02:53 AM

Here's an idea. Add a boolean that is false by default. When the dialogue pops up, have the boolean set to true, and false when the dialogue disappears. Then, only let the player move if the boolean is false.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

344 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I fix this movement issue? 1 Answer

Problem with UI Text displaying a score 0 Answers

How to play two animations with one UI button? 1 Answer

check 10 PlayerPrefs which is Empty and Save 1 Answer

Visual Scripting: No TextMeshPro Units 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges