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.