【发布时间】:2017-09-12 08:31:30
【问题描述】:
我是一名学习电子游戏编程的二年级学生,我一直在为这个问题苦苦挣扎一段时间,我想知道你们是否可以就如何解决这个问题提出最好的建议。
我有一个 3D 角色,他能够行走、奔跑、跳跃、二段跳和旋转以面对光标。但是,我注意到了一种错误,即我的角色能够控制其在半空中的移动。
例如,如果你按住左键 Shift + W 奔跑然后跳跃,你可以停止向前移动,然后在半空中开始向左扫射.
这是我的代码:
void Update ()
{
// Turns off all animations while in the air
if (isInAir)
{
animator.SetBool("Running", false);
animator.SetBool("Walking", false);
}
if (Input.GetKey(KeyCode.W))
{
// If the character is not in the air then turn on the walk animation
if (!isInAir)
{
animator.SetBool("Walking", true);
}
transform.Translate(Vector3.forward * movementSpeed * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.S))
{
// If the character is not in the air then turn on the walk animation
if (!isInAir)
{
animator.SetBool("Walking", true);
}
transform.Translate(Vector3.back * movementSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A))
{
// If the character is not in the air then turn on the walk animation
if (!isInAir)
{
animator.SetBool("Walking", true);
}
transform.Translate(Vector3.left * movementSpeed * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.D))
{
// If the character is not in the air then turn on the walk animation
if (!isInAir)
{
animator.SetBool("Walking", true);
}
transform.Translate(Vector3.right * movementSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.LeftShift))
{
// If the character is not in the air then turn on the run animation
// and change the character's movementSpeed
if (!isInAir)
{
movementSpeed = runSpeed;
animator.SetBool("Running", true);
}
}
else
{
// If the character is not in the air then reset their movement speed
// and turn off the run animation.
if (!isInAir)
{
movementSpeed = walkSpeed;
animator.SetBool("Running", false);
}
}
// When a key is released, turn off the movement animations
// This does not cause an issue since each movement if statement turns the animation back on
if (Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.S) || Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.D))
{
animator.SetBool("Walking", false);
animator.SetBool("Running", false);
}
#endregion
// Jumping
if (Input.GetButtonDown("Jump") && jumpCounter != 2)
{
jumpCounter++;
isInAir = true;
myRigidbody.AddForce(new Vector3(0, jumpForce));
if (jumpCounter == 1)
{
firstJumpStamp = Time.time;
}
}
if(Physics.Raycast(groundCheckRay, groundCheckRayDistance) && Time.time > firstJumpStamp + 0.5f)
{
jumpCounter = 0;
isInAir = false;
}
}
我已删除所有与角色移动无关的代码。
有人可以就我可以用来完成这项工作的方法给我一个建议吗?
我不是要代码来解决这个问题,只是一个建议......
我觉得我需要自己学习这个,我只需要有人指出我正确的方向。
如果您不理解部分代码,请随时问我,我很乐意向您展示我正在尝试用它做什么。 :) 例如,也许您知道编写某些行的更好方法。
(我确实尽量避免使用Input.GetAxis,因为当我没有 100% 控制它时,我发现它更难移动)
【问题讨论】:
-
你的扫射代码在哪里?
-
它只是检查 Input.GetKey(KeyCode.A) 和 KeyCode D,然后向左或向右翻译字符。该代码位于 S 检查的正下方