0

I have a game in a 3D space, and rigidbody comp. to a player. I have implemented a jump mechanic that is not working at intended, with this code:

using UnityEngine; public class PlayerMovement : MonoBehaviour { public bool Travel; public Rigidbody rb; public Transform tf; public bool Grounded; public float speed; int OnRail = 2; void Update () { float speed = 1f * Time.deltaTime; if (Input.GetKeyDown("d") && OnRail < 3) { tf.Translate(5.4f, 0f, 0f); OnRail++; } else if (Input.GetKeyDown("a") && OnRail > 1) { tf.Translate(-5.4f, 0f, 0f); OnRail--; } } void FixedUpdate () { if (Travel) { rb.velocity = new Vector3(0, 0, speed * Time.deltaTime); } if (Input.GetKeyDown("space") && Grounded) { Grounded = false; rb.AddForce(new Vector3(0, 500000f * Time.deltaTime, 0)); } if ((tf.position.y == 2f || tf.position.y == 10f) && rb.velocity.y == 0 && !Grounded) { Grounded = true; } } } 

The player is supposed to jump into the air, but only jumps almost no distance. The 2 and 10 Y positions are preset floors (I really don't need these 2 Y-coordinates. unless someone has a substitution for floor detection).

1
  • You can refer to this Unity Answers post to check how your player is grounded. Commented Mar 13, 2018 at 7:01

2 Answers 2

4

This is because GetKeyDown only occurs for one frame, meaning you are only applying a force upwards for that single frame. Additionally, Time.deltaTime is the amount of time it took to complete the last frame (a very small value), meaning you are applying a very small force upward. If you only want a single force applied on spacebar, there is no need to use Time.deltaTime here. Just do:

rb.AddForce(Vector3.up * 100f); // or some other reasonable multipliier 
Sign up to request clarification or add additional context in comments.

Comments

3

Hi for those that land on this looking for more information on their rigidbodies not behaving the way that they expected, I extracted bit of relevant information from Unity ForceMode's. Happy Developing.

 //Here, switching modes depend on button presses in the Game mode switch (m_ModeSwitching) { //This is the starting mode which resets the GameObject case ModeSwitching.Start: //This resets the GameObject and Rigidbody to their starting positions transform.position = m_StartPos; m_Rigidbody.transform.position = m_StartForce; //This resets the velocity of the Rigidbody m_Rigidbody.velocity = new Vector3(0f, 0f, 0f); break; //These are the modes ForceMode can force on a Rigidbody //This is Acceleration mode case ModeSwitching.Acceleration: //The function converts the text fields into floats and updates the Rigidbody’s force MakeCustomForce(); //Use Acceleration as the force on the Rigidbody m_Rigidbody.AddForce(m_NewForce, ForceMode.Acceleration); break; //This is Force Mode, using a continuous force on the Rigidbody considering its mass case ModeSwitching.Force: //Converts the text fields into floats and updates the force applied to the Rigidbody MakeCustomForce(); //Use Force as the force on GameObject’s Rigidbody m_Rigidbody.AddForce(m_NewForce, ForceMode.Force); break; //This is Impulse Mode, which involves using the Rigidbody’s mass to apply an instant impulse force. case ModeSwitching.Impulse: //The function converts the text fields into floats and updates the force applied to the Rigidbody MakeCustomForce(); //Use Impulse as the force on GameObject m_Rigidbody.AddForce(m_NewForce, ForceMode.Impulse); break; //This is VelocityChange which involves ignoring the mass of the GameObject and impacting it with a sudden speed change in a direction case ModeSwitching.VelocityChange: //Converts the text fields into floats and updates the force applied to the Rigidbody MakeCustomForce(); //Make a Velocity change on the Rigidbody m_Rigidbody.AddForce(m_NewForce, ForceMode.VelocityChange); break; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.