Unity3D-(难以置信的基本功能)可以跳转除东北

玩家的动作是不可思议的Input.GetaxisRaw基本动作。我从这个youtube tutorial获得了当前的跳跃系统。在我自己键入代码之前,我一定要了解代码。 无论哪种方式,我实现的所有其他跳转方法都存在此问题。我认为这可能与使用WD键给定的值有关。也许每个轴上的正值都覆盖了跳跃的能力?

这是我的Player游戏对象中的相关代码。

从变量开始:

    public float speed;
    public float gravity;
    public float jumpHeight = 3.0f;

    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;
    bool isGrounded;

    private CharacterController controller;
    private Vector3 velocity;
private void Move() {

        isGrounded = Physics.CheckSphere(groundCheck.position,groundDistance,groundMask);

         if (isGrounded && velocity.y < 0) {
            velocity.y = -2f;
        }

        float z = Input.GetaxisRaw("Vertical");
        float x = Input.GetaxisRaw("Horizontal");

        Vector3 move = transform.right * x + transform.forward * z;

        controller.Move(move * speed * Time.deltaTime);

        Jump();
    }

    private void Jump() {
        if (Input.GetKeyDown(KeyCode.E) && isGrounded) {
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }

        //This was my attempt at brute forcing the diagonal jump...didn't even run the Debug.Log().
        if (Input.GetKeyDown(KeyCode.E) && Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D) && isGrounded) { 

            Debug.Log("Moving Diagonal and attempting to jump");
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }
        //=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/
        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);
    }

我知道互联网上有很多业余Unity问题,但是我还没有找到针对我特定问题的解决方案。当然,存在“一般情况下不能对角跳跃”问题,但没有“不能在一个特定方向上沿对角跳跃”问题。

如果您有任何想法,请告诉我。

hubeihb 回答:Unity3D-(难以置信的基本功能)可以跳转除东北

一个可能的问题是 isGrounded 从未设置为true。

isGrounded = Physics.CheckSphere(groundCheck.position,groundDistance,groundMask); 以下代码要求groundDistance大于或等于groundCheck中心位置的距离。这是因为groundCheck.position没有考虑渲染范围。

您可能会想起

     if (isGrounded && velocity.y < 0) {
        velocity.y = -2f;
    }

被调用,CharacterController组件无需使用刚体就增加了重力 物理。

这是我测试过的代码。一切都按预期进行:

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

public class CharacterControllerInputDemo : MonoBehaviour
 {
public float speed =3;
public float gravity= -9;
public float jumpHeight = 3.0f;
public Transform groundCheck;
public float groundDistance =  1.5f;
public LayerMask groundMask;
//To view in the inspector for debugging
[SerializeField] bool isGrounded;
// allow assignment in the editor
[SerializeField] private CharacterController controller;
private Vector3 velocity;

void Start()
{
    if(controller == null)
    controller = GetComponent<CharacterController>();
}
private void Move()
{

    isGrounded = Physics.CheckSphere(groundCheck.position,groundMask);

    if (isGrounded && velocity.y < 0)
    {
        velocity.y = -2f;
    }

    float z = Input.GetAxisRaw("Vertical");
    float x = Input.GetAxisRaw("Horizontal");

    Vector3 move = transform.right * x + transform.forward * z;

    controller.Move(move * speed * Time.deltaTime);

    Jump();
}

private void Jump()
{
    if (Input.GetKeyDown(KeyCode.E) && isGrounded)
    {
        velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    }


    //=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/
    velocity.y += gravity * Time.deltaTime;
    controller.Move(velocity * Time.deltaTime);
}

void FixedUpdate()
{
    Move();
}
}
本文链接:https://www.f2er.com/3131019.html

大家都在问