Home c# Pause in 2D game

Pause in 2D game

Author

Date

Category

Made to play a pause on this , but there is a problem.

In the video, the character is not animated, and my opposite. And in the end, if (for example) jump and put a pause, then you can turn the character to the left-right, and also hear the sound jump if you press the space. How to block the character control at the time of pause? (Can offer better option).
Something from a series of if (time.timescale == 0F) {... code ...}

Character Management Code:

using unityengine;
Using System.Collections;
Public Class Playercontroller: Monobehaviour {
  Public Audiosource JumpCoundEffect;
  [Header ("Movement")]
  Public Float Jumpheight;
  Public Float MoveSpeed;
  Private Bool DoubleJumped;
  Private Float MoveVelocity;
  [Header ("Earth")]
  [Space (10)]
  Public Transform GroundCheck;
  Public Float GroundCheckradius;
  Public Layermask Whatisground;
  Private Bool Grounded;
  [Header ("Dropping")]
  [Space (10)]
  Public Float Knockback;
  Public Float KnockBackLength;
  Public Float KnockBackCount;
  Public Bool KnockbackFromRight;
  Private Animator Anim;
  // Use this for initialization
  Void Start ()
  {
    Anim = GetComponent & LT; Animator & GT; ();
  }
  Void FixedUpdate ()
  {
    Grounded = physics2d.overlapcircle (Groundcheck.position, GroundCheckradius, Whatisground);
  }
  // Update IS Called Once Per Frame
  Void Update ()
  {
    If (Grounded)
    {
      DoubleJumped = false;
    }
    Anim.SetBool ("Grounded", Grounded);
    If (input.GetKeyDown (keycode.space) & amp; & amp; grounded)
    {
      //Getcomponent< ;rigidbody2d>
      JUMP ();
    }
    If (input.GetKeyDown (keycode.space) & amp; & amp ;! DoubleJumped & amp; & amp;! Grounded)
    {
      //Getcomponent< ;rigidbody2d>
      JUMP ();
      DoubleJumped = True;
    }
    MoveVELOCITY = 0F;
    if (input.getkey (keycode.d))
    {
      //Getcomponent<rigidbody2d>
      MoveVELOCITY = MoveSpeed;
    }
    if (input.getKey (Keycode.a))
    {
      //Getcomponent<rigidbody2d>
      MoveVelocity = -MoveSpeed;
    }
    If (KnockBackCount & lt; = 0) {
      GetComponent & LT; Rigidbody2D & GT; () .Velocity = New Vector2 (MoveVelocity, GetComponent & LT; Rigidbody2D & GT; () .velocity.y);
    } else {
      if (knockbackfromright)
        GetComponent & LT; Rigidbody2D & GT; () .velocity = new vector2 (-knockback, knockback);
      if (! knockbackfromright)
        GetComponent & LT; Rigidbody2D & GT; () .Velocity = New Vector2 (Knockback, Knockback);
      knockbackcount - = time.deltatime;
    }
    var speed = mathf.abs (getComponent & lt; rigidbody2d & gt; () .velocity.x);
    Anim.setFloat ("Speed", Speed);
    if (Speed ​​& gt; 0.1f)
    {
      if (getComponent & lt; rigidbody2d & gt; (). Velocity.x & gt; 0)
        transform.localscale = new vector3 (8F, 8F, 8F);
      ELSE IF (GetComponent & LT; Rigidbody2D & GT; (). Velocity.x & lt; 0)
        transform.localscale = new vector3 (-8f, 8f, 8f);
    }
  }
  Public Void Jump ()
  {
    GetComponent & LT; Rigidbody2D & GT; (). Velocity = New Vector2 (GetComponent & LT; Rigidbody2D & GT; (). Velocity.x, JumpHeight);
    jumpsoundeffect.play ();
  }
}

Pause script code:

using unityengine;
Using System.Collections;
Public Class Pausemenu: Monobehaviour {
  Public String LevelSelect;
  Public String Mainmenu; 
Public bool ispaused;
  Public GameObject Pausemenucanvas;
  // Update IS Called Once Per Frame
  Void Update () {
    if (iSpaused) {
      Pausemenucanvas.SetActive (true); // Find out more detailed about setActive.
      Time.timescale = 0F;
    } else {
      Pausemenucanvas.SetActive (False);
      Time.timescale = 1f;
    }
    if (input.getKeydown (keycode.escape))
    {
      iSpaused =! Ispaused;
    }
  }
  Public Void Resume ()
  {
    ISPAUSED = FALSE;
  }
  Public Void LevelSelect ()
  {
    Application.Loadlevel (LevelSelect);
  }
  Public void quit ()
  {
    Application.Loadlevel (Mainmenu);
  }
}

Answer 1

At the very beginning of Update in the character control script insert the following.

if (time.timescale == 0F)
{
   Return;
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions