简体   繁体   English

无限跳跃统一3D

[英]Infinite jump in unity 3D

My character keeps floating up if space bar is held down until you let go, I tried all day on how to make the character to jump normal but I am just stuck.如果按住空格键直到您让 go 按住,我的角色会一直漂浮,我整天都在尝试如何让角色正常跳跃,但我只是卡住了。 I'm using unity to create the game.我正在使用统一来创建游戏。 The issue started when I changed onfloor=true, before when it was false the character could only jump once.当我更改 onfloor=true 时,问题就开始了,在它为 false 之前,角色只能跳一次。 Here is the code for the character in C#这是 C# 中字符的代码

Thank you谢谢

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
    private Transform rotateBody;
    private Vector3 Direction;
    private const float gravity = 0.1f;
    private const float jump_force = 0.09f;
    public Transform groundCheckTransform;
    private bool shiftKeyWasPressed;
    private bool jumpKeyWasPressed;
    private float horizontalInput;
    private Rigidbody RigidbodyComponent;
    public LayerMask playerMask;
    private int superJumpsRemaing;
    private bool on_floor = true;
    // Start is called before the first frame update
    void Start()
    {
        RigidbodyComponent = GetComponent<Rigidbody>();
        shiftKeyWasPressed = false;
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Space) & Direction.y > 0){
            jumpKeyWasPressed = true;

        }
        horizontalInput = Input.GetAxis("Horizontal");

        if(Input.GetKeyDown(KeyCode.LeftShift)){
            shiftKeyWasPressed = true;
        }
        horizontalInput = Input.GetAxis("Horizontal");

    }
 void FixedUpdate(){
Direction.y -= gravity;
Direction.x = 0;
if (Input.GetKey(KeyCode.Space)){
if (Direction.y < 0 && on_floor){
    Direction.y = 0;
}

if(on_floor){
            Direction.y += jump_force*2;
            on_floor = true;
}
}else{
    jumpKeyWasPressed = false;
}
if (Input.GetKey(KeyCode.D)){
    Direction.x += 2;
}
if (Input.GetKey(KeyCode.A)){
    Direction.x  -= 2;
    
    }
if (Input.GetKey(KeyCode.LeftShift))      

        if(shiftKeyWasPressed==true) 
            Direction.x *= 6;

        if(Physics.OverlapSphere(groundCheckTransform.position, 0.1f, playerMask).Length == 0){
        return;
        }
         if (jumpKeyWasPressed)
         {
             float jumpPower = 7;
             if (superJumpsRemaing > 0){
                 jumpPower *=2;
                 superJumpsRemaing--;
             }
            RigidbodyComponent.AddForce(Vector3.up*jumpPower, ForceMode.VelocityChange);
            jumpKeyWasPressed = false;
        }
    RigidbodyComponent.velocity = Direction;
    }
private void OnTriggerEnter(Collider other)
{

if ( other.gameObject.layer == 8){
    jumpKeyWasPressed = true;
    if(Direction.y > 8)
    jumpKeyWasPressed = false;
}

    if (other.gameObject.layer == 6){
        Destroy(other.gameObject);
    }
        if (other.gameObject.layer == 7){
        Destroy(other.gameObject);
        superJumpsRemaing++;
    }
}
}

Your on_floor variable never becomes false which results in constantly adding y value when ever space button is held down.您的 on_floor 变量永远不会变为 false,这会导致在按住空格按钮时不断添加 y 值。 You need to add the check for ground collision and set on_floor to false whenever player hits ground object.每当玩家撞到地面 object 时,您需要添加地面碰撞检查并将 on_floor 设置为 false。 You can use raycast towards downward direction for this something like:您可以为此使用向下方向的光线投射,例如:

    public dist = 10;
    public RaycastHit hit;
    public void CheckGroundBelow()
    {
           dist = 10; //Distance to calculate  height from
           dir = Vector3(0,-1,0); //Downward Direction
           if(Physics.Raycast(transform.position,dir,hit,dist)){
               on_floor = true;
           }
           else
           {
               on_floor  = false;
           }

    }

Here it checks if something is below your player's transform.在这里它检查是否有东西低于你的播放器的变换。 If there is something within 10 m then it means player is on ground otherwise it ignores input.如果 10 m 内有东西,则意味着玩家在地面上,否则它会忽略输入。 You can call it just above your if condition here you check for on_floor.您可以在此处检查 on_floor 的 if 条件上方调用它。

More about Raycast here: https://docs.unity3d.com/ScriptReference/Physics.Raycast.html有关 Raycast 的更多信息: https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

Alternatively you can also use Character Controller Component inside Unity which already has a check named as IsGrounded:或者,您也可以在 Unity 中使用 Character Controller 组件,该组件已经具有名为 IsGrounded 的检查:

More about Character controller here: https://docs.unity3d.com/ScriptReference/CharacterController.html有关字符 controller 的更多信息: https://docs.unity3d.com/ScriptReference/CharacterController.https:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM