简体   繁体   English

暂停不起作用(Unity c#)

[英]Pause don't works (Unity c#)

I writing runner game on Unity (C#) for mobile phones.我在 Unity (C#) 上为手机编写跑步游戏。

I made Pause button on screen, using Canvas - Button.我使用 Canvas - Button 在屏幕上制作了暂停按钮。

Also I made code for Pause in Platformer2DUserControl script.我还在 Platformer2DUserControl 脚本中为 Pause 编写了代码。

Here it is code of this script:这是这个脚本的代码:

   using UnityEngine;
using UnitySampleAssets.CrossPlatformInput;

namespace UnitySampleAssets._2D
{

    [RequireComponent(typeof(PlatformerCharacter2D))]
    public class Platformer2DUserControl : MonoBehaviour
    {
        private PlatformerCharacter2D character;
        private bool jump;
        public bool paused;

        private void Awake()
        {
            character = GetComponent<PlatformerCharacter2D>();
            paused = false;
        }

        private void Update()
        {

            /*if (Input.GetButton("Fire1"))
            {

            }*/


            if (!jump)
                // Read the jump input in Update so button presses aren't missed.
                 jump = Input.GetButton("Fire1"); //&& CrossPlatformInputManager.GetButtonDown("Jump"); 


        }

        private void FixedUpdate()
        {
            // Read the inputs.
            bool crouch = Input.GetKey(KeyCode.LeftControl);
            // float h = CrossPlatformInputManager.GetAxis("Horizontal");
            // Pass all parameters to the character control script.
            character.Move(1, false, jump);
            jump = false;

        }
        public void Pause()

        {
            if (!jump)
                // Read the jump input in Update so button presses aren't missed.
                jump = Input.GetButton("Fire1"); //&& CrossPlatformInputManager.GetButtonDown("Jump");

            paused = !paused;


            if (paused)
            {
                jump = !jump;
                Time.timeScale = 0;

            }
            else if (!paused)
            {
               // jump = Input.GetButton("Fire1");
                Time.timeScale = 1;
            }
        }
    }
}

My Pause Button is WORKS well.我的暂停按钮很好用。 But when I tap it , my character is jumping and game is pausing.但是当我点击它时,我的角色在跳跃,游戏暂停。

I want to make that , when I tapping the button game just pausing and character don't jump.我想做到这一点,当我点击按钮游戏时,只是暂停而角色不会跳跃。

How I can make it.我怎样才能做到。 Thank's for help.感谢帮助。

I would advise you not to use the same input for jumping and pausing.我建议您不要使用相同的输入进行跳跃和暂停。 Also, separate your jump and pause functionalities into separate functions.此外,将您的跳转和暂停功能分成不同的功能。 For pausing, create a UI button on the screen and make it call a public function on a Pause script, that will toggle pause.对于暂停,在屏幕上创建一个 UI 按钮并使其调用暂停脚本上的公共函数,这将切换暂停。 Then, in the same function, check if you are paused or not and adjust Time.timescale accordingly然后,在同一个函数中,检查你是否暂停并相应地调整 Time.timescale

You will have to attach the script with pause functionality on to an object that will always be in a screen (Say, a panel in your canvas or your MainCamera).您必须将具有暂停功能的脚本附加到始终位于屏幕中的对象(例如,画布或 MainCamera 中的面板)。 Under the button, add a new onClick() function after dragging the GO with the apt script to the box.在按钮下,将带有 apt 脚本的 GO 拖到框中后,添加一个新的 onClick() 函数。 Then, select the public function aforementioned.然后,选择上述公共功能。

private bool paused = false;

//The function called by the button OnClick()
public void TogglePause()
{
    paused = !paused;

    if(paused)
        Time.timescale = 0f;
    else
        Time.timescale = 1f;
}

Hope this helped!希望这有帮助!

Well, your code is messed up, but just remove the jump script from the pause method.好吧,您的代码搞砸了,但只需从 pause 方法中删除跳转脚本即可。 So it will just pause...所以它只会暂停......

public void Pause()

    {
        paused = !paused;

        if (paused)
        {
            Time.timeScale = 0;
        }
        else
        {
           Time.timeScale = 1;
        }
    }

Note that Input.GetButton should only be called in Update注意 Input.GetButton 应该只在 Update 中调用

EDIT编辑

The problem that you have to tap your screen to press the button.您必须点击屏幕才能按下按钮的问题。 And the code jump = Input.GetButton("Fire1");而代码 jump = Input.GetButton("Fire1"); basically means "Am I tapping the screen?"基本上意味着“我在点击屏幕吗?” So both JUMP and PAUSE are triggered.所以 JUMP 和 PAUSE 都被触发。

One solution would be to put a canvas filling the whole screen under your buttons.一种解决方案是在按钮下放置一个填充整个屏幕的画布。 You will trigger a JUMP action only when this canvas is clicked.只有在单击此画布时才会触发 JUMP 操作。 So when you click your pause button, it will stop the propagation and won't click the full screen canvas, which won't trigger the jump action.因此,当您单击暂停按钮时,它将停止传播并且不会单击全屏画布,这不会触发跳转动作。

EDIT2编辑2

Try changing the lines (in the Update function) :尝试更改行(在更新函数中):

if (!jump)
            // Read the jump input in Update so button presses aren't missed.
             jump = Input.GetButton("Fire1");

for :为了 :

if (!jump && !paused)
                // Read the jump input in Update so button presses aren't missed.
                 jump = Input.GetButton("Fire1");

I think the button event is called before the Update, see reference我认为按钮事件是在更新之前调用的, 请参阅参考

一种解决方案可以确保暂停按钮的光线投射。

I have always done it like this, essentially surround the entire fixed update (or wherever you have your game motion) with the paused-bool我一直都是这样做的,基本上用 paused-bool 包围整个固定更新(或任何你有游戏动作的地方)

    private void FixedUpdate()
    {
if (paused != true){
        // Read the inputs.
        bool crouch = Input.GetKey(KeyCode.LeftControl);
        // float h = CrossPlatformInputManager.GetAxis("Horizontal");
        // Pass all parameters to the character control script.
        character.Move(1, false, jump);
        jump = false;
}
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM