Search code examples
unity-game-enginegame-physics

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


I was hoping I could get some help! I've been trying to make a platformer using velocity to move but I can't find a good way to do jumping using the system. Every frame the velocity's y just resets itself and I don't know how to create a jump. I have tried using ForceMode.VelocityChange and I have tried to write out equations. The player falls extremely slowly even with gravity turned on. playerBody.AddForce(Vector3.up * jumpForce, ForceMode.VelocityChange);

I have the same issues when I try to set the y velocity to change with gravity

float MoveDirectionY = jumpForce * Physics.gravity.y;

enter image description here

Nothing seems to be working here. When i play the game gravity still pulls the object down slowly but if i turn off gravity it doesnt pull the object down at all.

The game does log the statement letting me know that it does know the space button was pressed.alt text

I want to also provide my code here:

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");
         }

     }
 }

Solution

  • Do not play with rigidbody in Update method. Use FixedUpdate() instead. In addition, do not change velocity using rb.velocity = ... but use rigibody.AddForce() method. Try something like this:

     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");
             }
         }