我如何在 Unity 中制作随着速度跳跃而移动的东西?
Posted
技术标签:
【中文标题】我如何在 Unity 中制作随着速度跳跃而移动的东西?【英文标题】:how do i make something which moves with Velocity jump in Unity? 【发布时间】:2020-05-29 01:56:32 【问题描述】:我希望我能得到一些帮助!我一直在尝试制作一个使用速度移动的平台游戏,但我找不到使用该系统进行跳跃的好方法。每一帧速度的 y 都会自行重置,我不知道如何创建跳跃。我尝试过使用 ForceMode.VelocityChange 并尝试写出方程式。即使在重力开启的情况下,玩家下落的速度也非常缓慢。
playerBody.AddForce(Vector3.up * jumpForce, ForceMode.VelocityChange);
当我尝试将 y 速度设置为随重力变化时,我遇到了同样的问题
float MoveDirectionY = jumpForce * Physics.gravity.y;
enter image description here
这里似乎没有任何工作。当我玩游戏时,重力仍然会缓慢地将物体拉下,但如果我关闭重力,它根本不会拉下物体。
游戏确实记录了让我知道它确实知道按下了空格按钮的语句。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");
【问题讨论】:
【参考方案1】:不要在更新方法中使用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");
【讨论】:
谢谢熊猫强!这帮助我解决了问题所在!一切都解决了!谢谢!以上是关于我如何在 Unity 中制作随着速度跳跃而移动的东西?的主要内容,如果未能解决你的问题,请参考以下文章