【发布时间】:2017-10-02 08:25:58
【问题描述】:
这是我在 Unity (2d) 中的玩家移动脚本。
当按下两个方向键时 - 而不是沿对角线移动 - 我需要玩家向最近按下的方向移动(如果释放,则为已经按下的方向)
if (!attacking)
{
if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
{
//transform.Translate (new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f ));
myRigidBody.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * currentMoveSpeed, myRigidBody.velocity.y);
PlayerMoving = true;
lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
}
if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
{
//transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
myRigidBody.velocity = new Vector2(myRigidBody.velocity.x, Input.GetAxisRaw("Vertical") * currentMoveSpeed);
PlayerMoving = true;
lastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
}
}
【问题讨论】:
-
添加一对私有变量,还记得之前的值并比较一下吗?您是在寻找有关如何处理此问题的基本想法,还是在编码时遇到问题?