【问题标题】:Unity 3D C# Player Movement Jumping ErrorUnity 3D C#玩家运动跳跃错误
【发布时间】:2019-04-03 16:07:46
【问题描述】:

所以我一直在编写一个脚本,让球在 x 轴上以恒定的速度移动,但也可以控制。当我按空格键跳跃时。除非我按 5 到 6 次按钮,否则它不会跳。如果你反复点击空格键,球第一次跳跃后它会跳跃,但如果你不理它,让它滚动,然后再尝试跳跃。它不会让你。我很困惑。

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

public class Speed : MonoBehaviour
{
    public float sspeed = 7.0F;
    public float jumpSpeed = 8.0F;
    public float gravity = 20.0F;

    private Vector2 moveDirection = Vector2.zero;

    void Start()
    {

    }
    void Update()
    {
        if (GetComponent<CharacterController>().isGrounded)
        {
            moveDirection = new Vector2();
            moveDirection = transform.TransformDirection(moveDirection);
            moveDirection *= sspeed;
            moveDirection.x = sspeed;
        }
        else if (GetComponent<CharacterController>())
        {
            moveDirection.x = gravity;
        }

        moveDirection.x -= gravity * Time.deltaTime;
        GetComponent<CharacterController>().Move(moveDirection * Time.deltaTime);
        CharacterController player = GetComponent<CharacterController>();

        if (GetComponent<CharacterController>().isGrounded)
        {
            moveDirection = new Vector2();
            moveDirection = transform.TransformDirection(moveDirection);
            moveDirection *= sspeed;

            if (Input.GetKeyDown("space"))
            {
                moveDirection.y = jumpSpeed;
            }
        }

        moveDirection.y -= gravity * Time.deltaTime;
        GetComponent<CharacterController>().Move(moveDirection * Time.deltaTime);
    }
}

【问题讨论】:

  • 你能把你的代码格式化得好一点吗?它真的很难阅读,因为它是

标签: c# unity3d


【解决方案1】:

有更好的方法:

Rigidbody body = GetComponent<Rigidbody>(); 

if(Input.GetKeyDown(KeyCode.Space)){
    body.AddForce(transform.up*jumpSpeed);
}

【讨论】:

    【解决方案2】:

    试试这个

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class ConstantMoveByX : MonoBehaviour
    {
        [SerializeField]
        private float constantSpeed = 10.0f;
        [SerializeField]
        private float jumpForce = 25.0f;
        [SerializeField]
        private float gravity = 9.8f;
    
        private CharacterController characterController;
        private Vector3 moveDirection = Vector3.zero;
    
        private void Start()
        {
            characterController = GetComponent<CharacterController>();
        }
    
        private void Update()
        {
            if (characterController.isGrounded)
            {
                moveDirection = Vector3.right * constantSpeed;
                moveDirection = transform.TransformDirection(moveDirection);
                moveDirection = moveDirection * constantSpeed;
    
                if (Input.GetKeyDown(KeyCode.Space))
                {
                    moveDirection.y = jumpForce;
                }
            }
    
            moveDirection.y = moveDirection.y - (gravity * Time.deltaTime);
            characterController.Move(moveDirection * Time.deltaTime);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多