我正在制作一个 2D 平台游戏,并按照教程来构建我的角色。这个角色工作正常,除了跳跃时,它不允许在空中改变方向。我如何添加这个以使我的角色能够在跳跃中改变方向?
用于基本运动的代码如下:
void Update()
{
CharacterController controller = GetComponent<CharacterController>();
float rotation = Input.GetAxis("Horizontal");
if(controller.isGrounded)
{
moveDirection.Set(rotation, 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
//running code
if(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) //check if shift is held
{ running = true; }
else
{ running = false; }
moveDirection *= running ? runningSpeed : walkingSpeed; //set speed
//jump code
if(Input.GetButtonDown("Jump"))
{
jump();
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
编辑:忘记包含 jump() 的代码,这可能很重要......
void jump()
{
moveDirection.y=jumpHeight;
transform.parent=null;
}