gpt4 book ai didi

c# - 暂停不起作用(Unity c#)

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:07:35 25 4
gpt4 key购买 nike

我在 Unity (C#) 上为手机编写跑酷游戏。

我使用 Canvas - Button 在屏幕上制作了暂停按钮。

我还在 Platformer2DUserControl 脚本中编写了暂停代码。

这是这个脚本的代码:

   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;
}
}
}
}

我的暂停按钮很好用。但是当我点击它时,我的角色在跳跃并且游戏在暂停。

我想做到这一点,当我点击按钮时,游戏只是暂停,角色不会跳跃。

我怎么做到的。感谢您的帮助。

最佳答案

我建议您不要将相同的输入用于跳跃和暂停。此外,将跳转和暂停功能分成不同的功能。对于暂停,在屏幕上创建一个 UI 按钮并使其调用暂停脚本上的公共(public)函数,这将切换暂停。然后,在同一个函数中,检查您是否暂停并相应地调整 Time.timescale

您必须将具有暂停功能的脚本附加到始终位于屏幕中的对象(例如, Canvas 或 MainCamera 中的面板)。在按钮下面,将带有apt脚本的GO拖到框内后,添加一个新的onClick()函数。然后,选择前面提到的公共(public)功能。

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;
}

希望这对您有所帮助!

关于c# - 暂停不起作用(Unity c#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36161594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com