- Home /
Question by
unity_17tvermeir · Sep 23, 2021 at 11:56 AM ·
2d2dplatformer
I want to pause my character when he is talking to another character. How can I do this?
I have just implemented a basic dialogue feature to my game and I don't like how the player is able to move when he is talking to an NPC. I have tried disabling my player controller script from my dialogue manager-script but that doesn't seem to work. Any workarounds on this? I will attach both my player controller script and my dialogue manager one. Thanks in advance!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public int amountOfJumps = 1;
private int facingDirection = 1;
private int amountOfJumpsLeft;
private int lastWallJumpDirection;
public Rigidbody2D rb;
public Animator anim;
public bool facingRight;
public bool isTouchingWall;
public bool canWallJump;
public bool canNormalJump;
public bool canMove;
public bool knockFromRight;
public bool canFlip;
public bool allowMovement;
private bool isWallSliding;
private bool isGrounded;
private bool isDashing;
private bool isAttemptingToJump;
private bool checkJumpMultiplier;
private bool hasWallJumped;
public float wallCheckDistance;
public float movementSpeed;
public float jumpForce = 10f;
public float mx;
public float wallSlideSpeed;
public float movementForceInAir;
public float airDragMultiplier = 0.95f;
public float variableJumpHeightMultipler = 0.5f;
public float wallHopForce;
public float wallJumpForce;
public float dashTime;
public float dashSpeed;
public float dashCoolDown;
public float groundCheckRadius;
public float knockback;
public float knockbackY;
public float knockbackCount;
public float knockbackLength;
public float jumpTimerSet = 0.15f;
public float turnTimerSet = 0.1f;
public float wallJumpTimerSet = 0.5f;
public float distanceBetweenImages;
public Vector2 wallHopDirection;
public Vector2 wallJumpDirection;
private float lastDash = -100f;
private float dashTimeLeft;
private float jumpTimer;
private float turnTimer;
private float wallJumpTimer;
private float lastImageXpos;
public Transform WallCheck;
[SerializeField] public Transform groundCheck;
[SerializeField] public LayerMask whatIsGround;
public void Start()
{
facingRight = true;
allowMovement = false;
canFlip = true;
wallHopDirection.Normalize();
wallJumpDirection.Normalize();
}
public void Update()
{
if (Mathf.Abs(rb.velocity.x) >= 0.01f && knockbackCount <= 0 && canMove)
{
anim.SetBool("isRunning", true);
}
else
{
anim.SetBool("isRunning", false);
}
CheckIfWallSliding();
CheckIfCanJump();
UpdateAnimations();
CheckJump();
}
private void UpdateAnimations()
{
anim.SetBool("isWallSliding", isWallSliding);
}
private void FixedUpdate()
{
CheckInput();
CheckSurroundings();
ApplyMovement();
CheckDash();
if (facingRight && mx < 0)
{
Flip();
}
else if (!facingRight && mx > 0)
{
Flip();
}
}
private void CheckInput()
{
mx = Input.GetAxis("Horizontal");
if (Input.GetButtonDown("Jump"))
{
if (isGrounded || (amountOfJumpsLeft > 0 && !isTouchingWall))
{
NormalJump();
}
else
{
jumpTimer = jumpTimerSet;
isAttemptingToJump = true;
}
}
if (Input.GetButtonDown("Horizontal")&& isTouchingWall)
{
if(!isGrounded&& mx != facingDirection)
{
canMove = false;
canFlip = false;
turnTimer = turnTimerSet;
}
}
if(!canMove)
{
turnTimer -= Time.deltaTime;
if (turnTimer <= 0)
{
canMove = true;
canFlip = true;
}
}
if (checkJumpMultiplier && !Input.GetButton("Jump"))
{
checkJumpMultiplier = false;
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * variableJumpHeightMultipler);
}
if (Input.GetButtonDown("Dash"))
{
if (Time.time >= (lastDash + dashCoolDown))
AttemptToDash();
}
}
private void CheckJump()
{
if(jumpTimer > 0)
{
if (!isGrounded && isTouchingWall && mx != 0 && mx != facingDirection)
{
WallJump();
}
else if (isGrounded)
{
NormalJump();
}
}
if(isAttemptingToJump)
{
jumpTimer -= Time.deltaTime;
}
if(wallJumpTimer > 0)
{
if(hasWallJumped && mx == -lastWallJumpDirection)
{
rb.velocity = new Vector2(rb.velocity.x, 0.0f);
hasWallJumped = false;
}
else if(wallJumpTimer <= 0)
{
hasWallJumped = false;
}
else
{
wallJumpTimer -= Time.deltaTime;
}
}
}
private void NormalJump()
{
if (canNormalJump)
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
amountOfJumpsLeft--;
jumpTimer = 0;
isAttemptingToJump = false;
checkJumpMultiplier = true;
}
}
private void WallJump()
{
if (canWallJump)
{
rb.velocity = new Vector2(rb.velocity.x, 0.0f);
isWallSliding = false;
amountOfJumpsLeft = amountOfJumps;
amountOfJumpsLeft--;
Vector2 forceToAdd = new Vector2(wallJumpForce * wallJumpDirection.x * mx, wallJumpForce * wallJumpDirection.y);
rb.AddForce(forceToAdd, ForceMode2D.Impulse);
jumpTimer = 0;
isAttemptingToJump = false;
checkJumpMultiplier = true;
turnTimer = 0;
canMove = true;
canFlip = true;
hasWallJumped = true;
wallJumpTimer = wallJumpTimerSet;
lastWallJumpDirection = -facingDirection;
}
}
private void AttemptToDash()
{
isDashing = true;
dashTimeLeft = dashTime;
lastDash = Time.time;
PlayerAfterImagePool.Instance.GetFromPool();
lastImageXpos = transform.position.x;
}
private void CheckDash()
{
if (isDashing)
{
if (dashTimeLeft > 0)
{
canFlip = false;
rb.velocity = new Vector2(dashSpeed * facingDirection, 0.0f);
dashTimeLeft -= Time.deltaTime;
if (Mathf.Abs(transform.position.x - lastImageXpos) > distanceBetweenImages)
{
PlayerAfterImagePool.Instance.GetFromPool();
lastImageXpos = transform.position.x;
}
}
if (dashTimeLeft <= 0 || isTouchingWall)
{
isDashing = false;
canMove = true;
canFlip = true;
}
}
}
private void CheckIfWallSliding()
{
if (isTouchingWall && mx == facingDirection && rb.velocity.y < 0)
{
isWallSliding = true;
}
else
{
isWallSliding = false;
}
}
private void CheckSurroundings()
{
isTouchingWall = Physics2D.Raycast(WallCheck.position, transform.right, wallCheckDistance, whatIsGround);
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
}
private void CheckIfCanJump()
{
if (isGrounded && rb.velocity.y <= 0.01f)
{
amountOfJumpsLeft = amountOfJumps;
}
if (isTouchingWall)
{
canWallJump = true;
}
if (amountOfJumpsLeft <= 0)
{
canNormalJump = false;
}
else
{
canNormalJump = true;
}
}
private void ApplyMovement()
{
if (canMove && knockbackCount <= 0)
{
rb.velocity = new Vector2(movementSpeed * mx, rb.velocity.y);
}
else
{
canMove = false;
if (knockFromRight)
{
rb.velocity = new Vector2(-knockback, knockbackY);
}
if(!knockFromRight)
{
rb.velocity = new Vector2(knockback, knockbackY);
}
knockbackCount -= Time.deltaTime;
}
if (isWallSliding)
{
if (rb.velocity.y < -wallSlideSpeed)
{
rb.velocity = new Vector2(rb.velocity.x, -wallSlideSpeed);
}
}
}
public void DisableFLip()
{
canFlip = false;
}
public void EnableFLip()
{
canFlip = true;
}
private void Flip()
{
if (!isWallSliding && canFlip)
{
facingRight = !facingRight;
transform.Rotate(0.0f, 180.0f, 0.0f);
facingDirection *= -1;
}
}
private void OnDrawGizmos()
{
Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
Gizmos.DrawLine(WallCheck.position, new Vector3(WallCheck.position.x + wallCheckDistance, WallCheck.position.y, WallCheck.position.z));
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Enemy")
{
knockbackCount = knockbackLength;
if (facingRight)
{
knockFromRight = true;
}
else if (!facingRight)
{
knockFromRight = false;
}
}
}
}
Dialogue Manager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class DialogueManager : MonoBehaviour
{
private Queue<string> sentences;
public TMP_Text nameText;
public TMP_Text dialogueText;
public Animator animator;
public PlayerMovement boolBoy;
public void Start()
{
sentences = new Queue<string>();
}
public void StartDialogue(Dialogue dialogue)
{
GameObject g = GameObject.FindGameObjectWithTag("Player");
boolBoy = g.GetComponent<PlayerMovement>();
animator.SetBool("isOpen", true);
boolBoy.canMove = false;
nameText.text = dialogue.names;
sentences.Clear();
foreach (string sentence in dialogue.sentences)
{
sentences.Enqueue(sentence);
}
DisplayNextSentence();
}
public void DisplayNextSentence()
{
if(sentences.Count == 0)
{
EndDialogue();
return;
}
string sentence = sentences.Dequeue();
StopAllCoroutines();
StartCoroutine(TypeSentence(sentence));
}
IEnumerator TypeSentence (string sentence)
{
dialogueText.text = "";
foreach(char letter in sentence.ToCharArray())
{
dialogueText.text += letter;
yield return null;
}
}
public void EndDialogue ()
{
Debug.Log("End of Conversation");
animator.SetBool("isOpen", false);
boolBoy.canMove = true;
}
}
Comment
Best Answer
Answer by AlgoUnity · Sep 24, 2021 at 07:13 AM
Does something like this work?
public bool paused = false;
public void Update()
{
if (paused)
return;
if (Mathf.Abs(rb.velocity.x) >= 0.01f && knockbackCount <= 0 && canMove)
{
anim.SetBool("isRunning", true);
}
else
{
anim.SetBool("isRunning", false);
}
CheckIfWallSliding();
CheckIfCanJump();
UpdateAnimations();
CheckJump();
}
private void FixedUpdate()
{
if (paused)
return;
...
}
I tried a variation of this and it worked, thanks for the help