Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 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
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
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;

      ...
  }
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 unity_17tvermeir · Sep 25, 2021 at 04:39 AM 0
Share

I tried a variation of this and it worked, thanks for the help

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

291 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

Related Questions

How to check for the ground in Unity 2D for Jumping 1 Answer

2D Animation does not start 1 Answer

How do I make a prefab ignore collisions with another prefab temporarily. 2 Answers

InputSystem v1.0.0 stops working sometimes 0 Answers

2D physics jump issue. 1 Answer


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