【问题标题】:Unity jump script delayunity跳转脚本延迟
【发布时间】:2021-07-09 16:12:22
【问题描述】:

我正在统一制作一个 3d 游戏,所以我制作了一个用于移动我的角色、行走和移动相机的 cs 脚本,但是当我添加跳转功能时,它有一个延迟。您可以按跳跃按钮 5 次,但没有结果。然后你再按一次,它就会跳起来。我不明白为什么会这样。

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

public class PlayerMovement : MonoBehaviour
{
    public CharacterController characterController;
    public int speed = 6;
    public float gravity = 9.87f;
    private float verticalspeed = 0;
    private Vector3 moveDirection = Vector3.zero;

    public Transform Camera;

    public float Sensitivity = 2f;
    public float uplimit = -50;
    public float downlimit = -50;
    public float jumpspeed = 5.0f;


    void Update()
    {
        move();
        cameramove();
        

        void cameramove()
        {
            float horizontal = Input.GetAxis("Mouse X");
            float vertical = Input.GetAxis("Mouse Y");

            transform.Rotate(0, horizontal * Sensitivity, 0);
            Camera.Rotate(-vertical * Sensitivity, 0, 0);

            Vector3 currentRotation = Camera.localEulerAngles;
            if (currentRotation.x > 180) currentRotation.x -= 360;
            currentRotation.x = Mathf.Clamp(currentRotation.x, uplimit, downlimit);
            Camera.localRotation = Quaternion.Euler(currentRotation);
        }

        void move()
        {
            float horizontalMove = Input.GetAxis("Horizontal");
            float verticalMove = Input.GetAxis("Vertical");

            if (characterController.isGrounded) verticalspeed = 0;
            else verticalspeed -= gravity * Time.deltaTime;

            Vector3 gravityMove = new Vector3(0, verticalspeed, 0);
            Vector3 move = transform.forward * verticalMove + transform.right * horizontalMove;
            characterController.Move(speed * Time.deltaTime * move + gravityMove * Time.deltaTime);
            if (characterController.isGrounded && Input.GetButton("Jump"))
            {
                moveDirection.y = jumpspeed;
            }
            moveDirection.y -= gravity * Time.deltaTime;
            characterController.Move(moveDirection * Time.deltaTime);



        }

    }
}

【问题讨论】:

    标签: c# unity3d visual-studio-2010 game-physics


    【解决方案1】:

    假设你的角色有一个RigidBody,你可以在你的脚本中引用它,

    public class PlayerMovement : MonoBehaviour
    {
        private Rigidbody myrigidbody;
    
        void Start () {
            myrigidbody = GetComponent<Rigidbody>();
        }
    }
    

    然后您可以将jumpspeed 作为Vector3 传递,并且您可能还必须在此处触发动画。

    if (characterController.isGrounded && Input.GetButton("Jump"))
    {
         characterController.isGrounded = false;
         myrigidbody.AddForce(new Vector3(0, jumpspeed, 0));
         // Trigger your animation, myanimator.SetTrigger("jump");
    }
    

    【讨论】:

    【解决方案2】:

    Unity角色控制器移动文档也包含跳转功能。

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Example : MonoBehaviour
    {
        private CharacterController controller;
        private Vector3 playerVelocity;
        private bool groundedPlayer;
        private float playerSpeed = 2.0f;
        private float jumpHeight = 1.0f;
        private float gravityValue = -9.81f;
    
        private void Start()
        {
            controller = gameObject.AddComponent<CharacterController>();
        }
    
        void Update()
        {
            groundedPlayer = controller.isGrounded;
            if (groundedPlayer && playerVelocity.y < 0)
            {
                playerVelocity.y = 0f;
            }
    
            Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
            controller.Move(move * Time.deltaTime * playerSpeed);
    
            if (move != Vector3.zero)
            {
                gameObject.transform.forward = move;
            }
    
            // Changes the height position of the player..
            if (Input.GetButtonDown("Jump") && groundedPlayer)
            {
                playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
            }
    
            playerVelocity.y += gravityValue * Time.deltaTime;
            controller.Move(playerVelocity * Time.deltaTime);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-13
      • 1970-01-01
      • 2012-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多