Whenever I start the unity editor and play it in the Game windows, sometimes the object can jump higher sometimes it is just small jump. I didn't change any value in my script nor changed it in inspector window.
It usually happens when I restart the unity editor or having my computer shutdown / restart and reopening the project.
Here are the jump code
void jump() {
if (isInGround) {
float currentJumpForce = jumpForce * Mathf.Abs(rigidBody.velocity.x) / 3.53f;
rigidBody.AddForce(Vector2.up * currentJumpForce);
animator.SetTrigger("isJump");
}
}
I place the code inside the FixedUpdate
method, for what I have read the physics calculation should be inside FixedUpdate
right?
private void FixedUpdate() {
if (Input.GetKeyDown(KeyCode.UpArrow)) {
jump();
}
}
I have read many are having similar problem, but it really doesn't answer this question.
UPDATE:
I forget to explain it in the question. The jumpForce
is calculated based on the velocity of the gameObject has. I capped the velocity to certain speed based on this code
void accelerate() {
if (rigidBody.velocity.x <= maxSpeed) {
rigidBody.AddForce(Vector2.right * acceletateForce);
}
}
where maxSpeed
is a constant. I know this is probably not the accurate way to cap the velocity, or is it? Maybe this is where it gets messed up?
I just kinda assume the velocity is the capped from the camera movement and the 'display' or the movement of the screen. It seems the same to me.
currentJumpForce
calculation depends onrigidBody.velocity.x
, which means the faster the object is moving to the +x or -x axis, the higher the object will jump. Is there a reason for this? Is it a game feature? \$\endgroup\$ – TomTsagk Aug 22 '18 at 13:10