Unity 2d 运动脚本问题
Unity 2d Movement script issue
所以我正在 Unity 中制作一个 2d 平台游戏(对 c# 和 Unity 还是新手),我正在尝试为一个简单的正方形制作一个移动脚本,正方形会随机停止移动,我将不得不跳起来重新开始移动,只为再次发生。
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed;
public float jumpHeight;
void Start()
{
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
}
if (Input.GetKey(KeyCode.D))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(moveSpeed, 0);
}
if (Input.GetKey(KeyCode.A))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(-moveSpeed, 0);
}
}
}
如果您向我们提供了实际使用的代码,@Bejasc 的评论可能会提供正确答案。这里有一些您没有要求的提示,可以在清洁度、最佳实践和某些功能方面改进您的代码:
- 不要在每次要读取值时都使用 GetComponent<>()。创建游戏对象时将其保存在变量中! (检查下面代码中的 Start() 方法)
- 如果玩家正在移动,你将 Y-velocity 设置为 0,如果你想同时跳跃和移动,这将不起作用,如果你跳跃(将 Y 速度设置为 jumpHeight)然后移动(设置Y 速度为 0)你的角色将漂浮在半空中,因为我们每帧都将 Y 速度设置为 0。改为将其设置为当前的 Y 速度! (移动时勾选
new Vector2
中的y参数)
结果代码:
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed;
public float jumpHeight;
Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
rb.velocity = new Vector2(rb.velocity.x, jumpHeight);
}
if (Input.GetKey(KeyCode.D))
{
rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
}
if (Input.GetKey(KeyCode.A))
{
rb.velocity = new Vector2(-moveSpeed, rb.velocity.y);
}
}
}
对于这个简单的移动脚本,您还可以通过以下方式大量简化移动代码:
(假设您在 Unity 输入设置中使用标准输入设置(编辑 -> 项目设置 -> 输入))
Input.GetAxis("Horizontal") 将是 -1
如果按下 A
、left arrow
或 left on a gamepad joystick
并且 1
如果 D
、right arrow
或 right on a gamepad joystick
被按下。这是 "Horizontal" 的默认设置,我想您可以猜出 "Vertical" 的作用。
void Update() {
float moveDir = Input.GetAxis("Horizontal") * moveSpeed;
rb.velocity = new Vector2(moveDir, rb.velocity.y);
// Your jump code:
if (Input.GetKeyDown(KeyCode.Space))
{
rb.velocity = new Vector2(rb.velocity.x, jumpHeight);
}
}
如果您有任何疑问或这是否有帮助,请告诉我。
我让它工作的唯一方法是在时间管理器中将固定时间步长更改为 0.0166。似乎物理引擎和更新不同步结果卡顿。
所以我正在 Unity 中制作一个 2d 平台游戏(对 c# 和 Unity 还是新手),我正在尝试为一个简单的正方形制作一个移动脚本,正方形会随机停止移动,我将不得不跳起来重新开始移动,只为再次发生。
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed;
public float jumpHeight;
void Start()
{
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
}
if (Input.GetKey(KeyCode.D))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(moveSpeed, 0);
}
if (Input.GetKey(KeyCode.A))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(-moveSpeed, 0);
}
}
}
如果您向我们提供了实际使用的代码,@Bejasc 的评论可能会提供正确答案。这里有一些您没有要求的提示,可以在清洁度、最佳实践和某些功能方面改进您的代码:
- 不要在每次要读取值时都使用 GetComponent<>()。创建游戏对象时将其保存在变量中! (检查下面代码中的 Start() 方法)
- 如果玩家正在移动,你将 Y-velocity 设置为 0,如果你想同时跳跃和移动,这将不起作用,如果你跳跃(将 Y 速度设置为 jumpHeight)然后移动(设置Y 速度为 0)你的角色将漂浮在半空中,因为我们每帧都将 Y 速度设置为 0。改为将其设置为当前的 Y 速度! (移动时勾选
new Vector2
中的y参数)
结果代码:
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed;
public float jumpHeight;
Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
rb.velocity = new Vector2(rb.velocity.x, jumpHeight);
}
if (Input.GetKey(KeyCode.D))
{
rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
}
if (Input.GetKey(KeyCode.A))
{
rb.velocity = new Vector2(-moveSpeed, rb.velocity.y);
}
}
}
对于这个简单的移动脚本,您还可以通过以下方式大量简化移动代码:
(假设您在 Unity 输入设置中使用标准输入设置(编辑 -> 项目设置 -> 输入))
Input.GetAxis("Horizontal") 将是 -1
如果按下 A
、left arrow
或 left on a gamepad joystick
并且 1
如果 D
、right arrow
或 right on a gamepad joystick
被按下。这是 "Horizontal" 的默认设置,我想您可以猜出 "Vertical" 的作用。
void Update() {
float moveDir = Input.GetAxis("Horizontal") * moveSpeed;
rb.velocity = new Vector2(moveDir, rb.velocity.y);
// Your jump code:
if (Input.GetKeyDown(KeyCode.Space))
{
rb.velocity = new Vector2(rb.velocity.x, jumpHeight);
}
}
如果您有任何疑问或这是否有帮助,请告诉我。
我让它工作的唯一方法是在时间管理器中将固定时间步长更改为 0.0166。似乎物理引擎和更新不同步结果卡顿。