如何加上「;“跳跃”;在Unity3d的C#脚本中使用角色控制器?

如何加上「;“跳跃”;在Unity3d的C#脚本中使用角色控制器?,c#,windows,unity3d,game-development,C#,Windows,Unity3d,Game Development,我使角色可以朝8个方向行走,但我不知道如何添加一个“跳跃”使一切正常 using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovement : MonoBehaviour { public CharacterController controller; public float speed; float turnSmoothV

我使角色可以朝8个方向行走,但我不知道如何添加一个“跳跃”使一切正常

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

public class PlayerMovement : MonoBehaviour {

    public CharacterController controller;
    public float speed;
    float turnSmoothVelocity;
    public float turnSmoothTime;

    void Update() {
        float horizontal = Input.GetAxisRaw("Horizontal");
        float vertical = Input.GetAxisRaw("Vertical");

        Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;

        if (direction.magnitude >= 0.1f) {
            float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
            float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
            transform.rotation = Quaternion.Euler(0f, angle, 0f); 
            controller.Move(direction * speed * Time.deltaTime);
        }
    }
}

无需计算角色的角度和旋转,因为在使用CharacterController类时,这些已由Unity为您计算

要跳转,您可能需要为跳转操作指定一个按钮。然后,您可以在
Update
中检查是否为每一帧按下了跳转按钮。 您可以使用类似的方法,并将其添加到代码中的移动命令中:

 public float jumpSpeed = 2.0f;
 public float gravity = 10.0f;
 private Vector3 movingDirection = Vector3.zero;

 void Update() {
     if (controller.isGrounded && Input.GetButton("Jump")) {
         movingDirection.y = jumpSpeed;
     }
     movingDirection.y -= gravity * Time.deltaTime;
     controller.Move(movingDirection * Time.deltaTime);
 }

试着寻求一些帮助或解释,而不仅仅是让别人帮你做这项工作。我们希望人们在这个社区学习,而不仅仅是得到答案:)那么你能就如何解决我的问题给出建议吗?;)@似乎有些运营商甚至不理解这样一条评论的含义^^他们试图说的是:你的问题在这里是离题的,因为你需要一个完整的解决方案/库/工具。请参阅