如何使用此代码使我的角色两次跳转?(新手)

时间:2019-08-15 17:09:25

标签: c# unity3d

我想跳两次,但没什么主意!到目前为止,仅管理了一次跳跃。 遵循了一些youtube教程和一些文章,但是它们都不对我有用。我知道我做错了,因为我对c#不熟悉。

using System.Collections;
using System.Collections.Generic; 
using UnityEngine;

public class Walk_script : MonoBehaviour 
{
  public float speed = 4;
  public float gravity = 8;
  public float jumpSpeed = 5.0f;

  Vector3 moveDirection = Vector3.zero;
  CharacterController controller;
  Animator anim;

  // Start is called before the first frame update
  void Start()
  {
    controller = GetComponent<CharacterController>();
    anim = GetComponent<Animator>();
  }

  // Update is called once per frame
  void Update()
  {
    if (controller.isGrounded)
    {
      if (Input.GetKey(KeyCode.W))
      {
        anim.SetBool("running", true);
        moveDirection = new Vector3(0, 0, 1);
        moveDirection *= speed;
      }

      if (Input.GetKeyUp(KeyCode.W))
      {
        anim.SetBool("running", false);
        moveDirection = new Vector3(0, 0, 0);
      }

      if (Input.GetKeyDown(KeyCode.Space))
      {
        anim.SetBool("jump", true);
        moveDirection.y = jumpSpeed;
      }
      else
      {
        anim.SetBool("jump", false);
      }
    }
    moveDirection.y -= gravity * Time.deltaTime;
    controller.Move(moveDirection * Time.deltaTime);
  }
}

1 个答案:

答案 0 :(得分:2)

您可以签出this答案以获取正确的跳转和两次跳转实现,但这是您的两次跳转代码。

您需要管理hasJumped布尔值,以检查是否可以在空中再次跳跃。 在地面上跳跃时将其设置为true,并检查其是否正确,以便您只能在空中再次跳跃一次。

|  id   |   name  |
+-------+---------+
|   1   | newyork |
|   2   | london  |
|  ...  |   ...   |
|  100  | istanbul|