【发布时间】:2014-10-28 22:00:21
【问题描述】:
我正在尝试找到一种在 Unity 中暂停我的游戏而不使用“Time.timeScale = 0;”的方法。我想改变它的原因是我想让角色在游戏暂停时播放动画。有没有办法改变这个脚本,使重力和前进速度只在玩家点击空间后“设置”?或者有没有更好的方法来解决这个问题?
这是脚本:
public class PlayerMove : MonoBehaviour {
Vector3 velocity = Vector3.zero;
public Vector3 gravity;
public Vector3 FlyVelocity;
public float maxSpeed = 5f;
public float forwardSpeed = 1f;
bool didFly = false;
bool dead = false;
float deathCooldown;
Animator animator;
// Use this for initialization
void Start () {
animator = GetComponentInChildren<Animator>();
}
// Do Graphic & Input updates here
void Update(){
if (dead) {
deathCooldown -= Time.deltaTime;
if (deathCooldown <= 0) {
if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0) ) {
Application.LoadLevel( Application.loadedLevel );
}
}
}
if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0) ) {
didFly = true;
animator.SetTrigger ("DoFly");
}
}
void OnCollisionEnter2D(Collision2D collision) {
animator.SetTrigger ("Death");
dead = true;
}
// Do physics engine updates here
void FixedUpdate () {
if (dead)
return;
velocity += gravity * Time.deltaTime;
velocity.x = forwardSpeed;
if(didFly == true) {
didFly = false;
velocity += FlyVelocity;
}
velocity = Vector3.ClampMagnitude(velocity, maxSpeed);
transform.position += velocity * Time.deltaTime;
deathCooldown = 0.5f;
}
}
【问题讨论】:
-
您可以在 gamedev.stackexchange.com 上找到更多关于统一的帮助
-
没有任何东西可以集成到统一中。但我建议看看状态机,因为当你有不同的状态(暂停/播放)以及进入/退出状态时需要做的事情时,它们会有所帮助。
-
我知道这个帖子有点老了,但你可以在这里试试我的答案:stackoverflow.com/questions/31718168/pausing-in-unity/…
标签: c# unity3d 2d-games unity3d-2dtools