当我统一按下空格时,什么都没有发生

时间:2020-03-22 15:15:52

标签: c# unity3d

我正在做我的第一个统一项目。我想添加跳跃,但是当我按下空格键时什么也没发生。我正在使用C#。我检查了,输入应该没问题。谁能帮忙吗?

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

public class pmovr : MonoBehaviour
{
    public Vector3 jump;
    public float jumpForce = 20.0f;
    public bool isGrounded;
    Rigidbody rb;   
    public CharacterController pmove;
    public float speed = 12f;
    public float grav = -9.81f;
    private Vector3 velocity;
    void Start(){
        rb = GetComponent<Rigidbody>();
        jump = new Vector3(0.0f, 2.0f, 0.0f);
        }
    void OnCollisionStay()
        {
        isGrounded = true;
        }
    void Update()
    {
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        Vector3 movement = transform.right * x + transform.forward * z;
        pmove.Move(movement * speed * Time.deltaTime);
        velocity.y += grav * Time.deltaTime;
        pmove.Move(velocity * Time.deltaTime);      
        if(Input.GetKeyDown(KeyCode.Space) && isGrounded){
            rb.AddForce(jump * jumpForce, ForceMode.Impulse);
            isGrounded = false;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我已经尝试过您的脚本,但遇到以下问题:

  • CharacterController脚本的构建无需刚性体即可工作,因此 施加的力不会影响。

  • CharacterController有它自己的对撞机,所以要添加更多(I
    不知道您是否这样做,但仅涵盖可能性)可能会导致 检查它是否接地的问题。

我的建议是删除您的CharacterController脚本,并继续对其行为进行编码。如果您做到了跳跃,那么移动对您来说将很容易!

无论如何,作为建议,请尝试分别调试变量。 当您按空格键时,打印“ isGrounded”的值(如果为true),检查rb的状态,等等。