[英]Time.Timescale is not pausing the game
这是我正在制作的球类游戏的脚本。 游戏应该在达到Time.timescale
时暂停,但事实并非如此。
我需要知道我做错了什么以及我需要在这里改变什么。
我是 Unity 和 C# 的初学者,所以我可能把一切都搞砸了。
如果有任何其他方式来暂停游戏而不是使用Time.timescale
我也想知道。
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
public class PauseScript : MonoBehaviour
{
public bool paused = false;
void Start()
{
paused = false;
Time.timeScale = 1;
}
// Update is called once per frame
public void Pause()
{
if (Input.GetKeyDown("p") && paused == false)
{
Time.timeScale = 0;
paused = true;
}
if (Input.GetKeyDown("p") && paused == true)
{
Time.timeScale = 1;
paused = false;
}
}
}
球.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
public class Ball : MonoBehaviour
{
//configuration parameter
[SerializeField] Paddle paddleInitial;
[SerializeField] float horizontalPush = 2f;
[SerializeField] float verticalPush = 15f;
[SerializeField] AudioClip[] ballSounds;
//state parameter
Vector2 paddleToBallVector;
bool mouseButtonClicked = false;
//Cached COmponent References
AudioSource myAudioSource;
// Start is called before the first frame update
void Start()
{
paddleToBallVector = transform.position - paddleInitial.transform.position;
myAudioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (mouseButtonClicked == false)
{
stickBallToPaddle();
launchOnMouseClick();
}
if (Input.GetMouseButtonDown(1))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(horizontalPush,verticalPush);
}
}
private void launchOnMouseClick()
{
if (Input.GetMouseButtonDown(0))
{
mouseButtonClicked = true;
GetComponent<Rigidbody2D>().velocity = new Vector2(horizontalPush, verticalPush);
}
}
private void stickBallToPaddle()
{
Vector2 paddlePosition = new Vector2(paddleInitial.transform.position.x, paddleInitial.transform.position.y);
transform.position = paddlePosition + paddleToBallVector;
}
//audio settings
private void OnCollisionEnter2D(Collision2D collision)
{
if (mouseButtonClicked)
{
AudioClip clip = ballSounds[UnityEngine.Random.Range(0, ballSounds.Length)];
myAudioSource.PlayOneShot(clip);
}
}
}
有几个潜在的问题。
Update
,否则它不会每帧运行一次Input.GetKeyDown
返回是否已开始按下某个键,但这会持续存在于当前帧上(仅在下一次Update
调用之前重置,因此每次在同一Update
function 块中调用它时都会返回 true)当您按下 P 时,您检查键是否按下然后设置暂停标志/时间刻度,但是如果检查执行相反操作的更新方法,则执行另一个操作。
// Update is called once per frame
public void Pause()
{
// The p key is down and the game isn't paused, set timescale to 0 and paused to true...
if (Input.GetKeyDown("p") && paused == false)
{
Time.timeScale = 0;
paused = true;
}
// The p key is still down since we are on the same frame and paused flag is set so here we set timescale to 1 and paused to false...
if (Input.GetKeyDown("p") && paused == true)
{
Time.timeScale = 1;
paused = false;
}
}
您可能希望确保每次更新只运行这些条件之一,并确保 function 被称为Update
,否则它不会每帧运行一次
// Update is called once per frame
public void Update()
{
if (Input.GetKeyDown("p") && paused == false)
{
Time.timeScale = 0;
paused = true;
}
// Use else to make sure this block only gets executed if the above doesn't
else if (Input.GetKeyDown("p") && paused == true)
{
Time.timeScale = 1;
paused = false;
}
}
可以这么说,给猫剥皮的方法有很多种。 替代代码可能如下所示:
public void Update()
{
if(Input.GetKeyDown("p"))
{
// Toggle paused
paused != paused;
// Use ternary operator to save lines because making code harder to read is fun
Time.timeScale = paused ? 0 : 1;
}
}
- 编辑:
还要确保将运动等的任何矢量乘以每帧的deltaTime
- 否则您的模拟将继续。
您可能还需要为其他方面(如动画等)显式处理暂停 - 这取决于它们的实现方式,但只要他们逐帧考虑增量,它们就会在timeScale
设置为 0 时做出反应,如预期的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.