我如何在 Unity 中制作随 Velocity 跳跃移动的东西?

how do i make something which moves with Velocity jump in Unity?

我希望能得到一些帮助!我一直在尝试制作一个使用速度移动的平台游戏,但我找不到使用该系统进行跳跃的好方法。速度的 y 每一帧都会自行重置,我不知道如何创建跳跃。我曾尝试使用 ForceMode.VelocityChange 并尝试写出方程式。即使启用了重力,玩家的下落速度也非常缓慢。 playerBody.AddForce(Vector3.up * jumpForce, ForceMode.VelocityChange);

当我尝试将 y 速度设置为随重力变化时,我遇到了同样的问题

float MoveDirectionY = jumpForce * Physics.gravity.y;

enter image description here

这里似乎没有任何效果。当我玩游戏时,重力仍然会缓慢地向下拉物体,但如果我关闭重力,它根本不会向下拉物体。

游戏确实记录了让我知道它确实知道 space 按钮是 pressed.alt 文本

的声明

我也想在这里提供我的代码:

using System.Collections;
 using System.Collections.Generic;
 using System.Transactions;
 using Unity.Collections.LowLevel.Unsafe;
 using UnityEngine;

 public class PlayerMovement : MonoBehaviour
 {
     [SerializeField] private Rigidbody playerBody;
     [SerializeField] private Vector3 inputVector;
     [SerializeField] public float speed = 0.01f;
     [SerializeField] public bool jump;
     [SerializeField] private float turnSpeed = 45;
     [SerializeField] public float jumpForce = 35000f;
     [SerializeField] private bool isOnGround = true;
     [SerializeField] float enemyPushForce = 100;
     public int ingredient;
     public GameManager gameManager;
     public camSwitch cs;
     public float horizontalInput;
     public float verticalInput;
     float playerFacingAngleY;
     private GameObject FocalPoint;


     // Start is called before the first frame update
     void Start()
     {
         //Just making sure we have the rigid body of the game object the script is attached to so we can move it later
         playerBody = gameObject.GetComponent<Rigidbody>();
         FocalPoint = GameObject.Find("Focal Point");
     }

     // Update is called once per frame
     //This is where the player script should be realizing we are using inputs
     void Update()
     {
         horizontalInput = Input.GetAxis("Horizontal");
         verticalInput = Input.GetAxis("Vertical");
         playerFacingAngleY += horizontalInput * turnSpeed;
         Vector3 playerFacingDirection = new Vector3(0, playerFacingAngleY, 0);
         playerBody.rotation = Quaternion.Euler(playerFacingDirection);
         float moveDirectionX = (FocalPoint.transform.position.x - gameObject.transform.position.x) *speed * verticalInput * Time.deltaTime;
         float MoveDirectionY = jumpForce * Physics.gravity.y;
         float moveDirectionZ = (FocalPoint.transform.position.z - gameObject.transform.position.z) * speed * verticalInput * Time.deltaTime;
         Vector3 moveDirection = new Vector3(moveDirectionX, MoveDirectionY, moveDirectionZ);



         playerBody.velocity = moveDirection;

         if (Input.GetKeyDown(KeyCode.Space) && isOnGround == true)
         {
             playerBody.AddForce(Vector3.up * jumpForce, ForceMode.VelocityChange);
             isOnGround = false;
             print("player has jumped");
         }

     }

     private void OnCollisionEnter(Collision collision)
     {
         isOnGround = true;
         if (collision.gameObject.tag == "Enemy")
         {
             Debug.Log("Player ran into an enemy");
             if (cs.inSky == true)
             {
                 speed = 0;
             }
             else
             {
                 speed = 10;
             }
         }
         else if (collision.gameObject.tag == "Ingredient")
         {

             Debug.Log("Player collided with an ingredient");
             collision.gameObject.SetActive(false);
             ingredient++;
         }
         else if (collision.gameObject.tag == "Ground") {
             isOnGround = true;
             print("player has hit the ground");
         }

     }
 }

不要在 Update 方法中使用 rigidbody。请改用 FixedUpdate()。 另外,不要使用rb.velocity = ...改变速度,而是使用rigibody.AddForce()方法。尝试这样的事情:

 void FixedUpdate() //using rigidbody? => ONLY FIXEDUPDATE
     {
         horizontalInput = Input.GetAxis("Horizontal");
         verticalInput = Input.GetAxis("Vertical");
         playerFacingAngleY += horizontalInput * turnSpeed;
         Vector3 playerFacingDirection = new Vector3(0, playerFacingAngleY, 0);
         playerBody.rotation = Quaternion.Euler(playerFacingDirection);
         float moveDirectionX = (FocalPoint.transform.position.x - gameObject.transform.position.x) *speed * verticalInput * Time.deltaTime;
         float moveDirectionZ = (FocalPoint.transform.position.z - gameObject.transform.position.z) * speed * verticalInput * Time.deltaTime;
         Vector3 moveDirection = new Vector3(moveDirectionX, 0.0f, moveDirectionZ); //0.0f - just turn on gravity in rigidbody component or you can change it if you want some additional Vertical force
         playerBody.AddForce(moveDirection, ForceMode.VelocityChange); //force mode change to whatever you want 

         if (Input.GetKeyDown(KeyCode.Space) && isOnGround == true)
         {
             playerBody.AddForce(Vector3.up * jumpForce, ForceMode.VelocityChange);
             isOnGround = false;
             print("player has jumped");
         }
     }