【问题标题】:Infinite jump in unity 3Dunity 3D 无限跳跃
【发布时间】:2021-11-21 06:15:10
【问题描述】:

如果按住空格键直到你松开,我的角色会一直漂浮,我整天都在尝试如何让角色正常跳跃,但我只是卡住了。我正在使用统一来创建游戏。当我更改 onfloor=true 时,问题就开始了,在它为 false 之前,角色只能跳一次。这是C#中字符的代码

谢谢

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++;
    }
}
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    您的 on_floor 变量永远不会变为 false,这会导致在按住空格按钮时不断添加 y 值。您需要添加地面碰撞检查,并在玩家撞击地面物体时将 on_floor 设置为 false。您可以为此使用向下方向的光线投射,例如:

        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;
               }
    
        }
    

    在这里它检查是否有东西低于你的玩家的 transform 。如果 10 m 内有东西,则意味着玩家在地面上,否则它会忽略输入。您可以在此处检查 on_floor 的 if 条件上方调用它。

    更多关于 Raycast 的信息在这里:https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

    或者你也可以在 Unity 中使用 Character Controller 组件,它已经有一个名为 IsGrounded 的检查:

    更多关于角色控制器的信息在这里:https://docs.unity3d.com/ScriptReference/CharacterController.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多