[英]Vector2 unity2d movement is not smooth when "space" is still being hold
我正在制作一个 Unity 2D 游戏,但我有一个脚本,其中玩家使用 vector2 上升并返回但如果它下降并且我仍然保持空间它不再平滑 go 我该如何解决?
using UnityEngine;
using System.Collections;
public class playercontrol : MonoBehaviour {
void Update ()
{
Movment ();
}
void Movment()
{
if (Input.GetKey (KeyCode.D))
{
transform.Translate(Vector2.right *4f* Time.deltaTime);
transform.eulerAngles = new Vector2(0, 0);
}
if (Input.GetKey (KeyCode.A))
{
transform.Translate(Vector2.left *4f* Time.deltaTime);
transform.eulerAngles = new Vector2(0, 0);
}
if (Input.GetKey (KeyCode.Space))
{
transform.Translate(Vector2.up *4f* Time.deltaTime);
transform.Translate(Vector2.up *4f* Time.deltaTime);
transform.eulerAngles = new Vector2(0, 0);
}
}
}
正如其他人所提到的,这是 Unity 的常见现象,最好的解决方案是使用 RigidBody 处理运动......
但是,还有其他解决方法,有些不太优雅,但有些还可以。
使用原始输入。
// Get the raw inputs
var x = Input.GetAxis("Horizontal");
var jump = Input.GetAxis("Jump");
var move = new Vector2(x, 0);
// Move horizontally
transform.Translate(move);
// Handle Jump
if (jump > 0) { ... }
您需要做的是检查组合输入,然后从 function return
。
// Handle multiple input first
if(Input.GetKey (KeyCode.D) && Input.GetKey (KeyCode.Space))
{
...
return;
}
if(Input.GetKey (KeyCode.A) && Input.GetKey (KeyCode.Space))
{
...
return;
}
if(Input.GetKey (KeyCode.D)) { ... }
if(Input.GetKey (KeyCode.A)) { ... }
if(Input.GetKey (KeyCode.Space)) { ... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.