- Home /
AddForce jump height dimities on level reload,Add force Jump height diminishes on every level reload.
Hi,
I am new to unity and I'm working on the create with code projects and on prototype 3 we have a character that jumps to avoid obstacle. Now, I decided to add a simple UI which includes a restart button. My problem is that every time a reload (restart) the game/current level/scene, the force of the jump diminishes until the character does not jump anymore. Does anybody have encounter a similar issues?
Answer by mrmatt1877 · Mar 01, 2021 at 01:11 AM
That depends on how you are restarting the scene. If you are reloading using load scene, then it should restart with the same properties it started with. Unless you have these values stored in an object that isn't destroyed on load.
If you are doing something else then I would need to see how you are resetting the player's position and velocity.
This is the original Player Controller Script from the tutorial. I added a few lines for the gameover text and the restart button. For the reload scene, I'm using the following code:
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI;
public class PlayerController : MonoBehaviour { private Rigidbody playerRb; private Animator playerJumpAnimation; public ParticleSystem explosion; public ParticleSystem groundDirt; private AudioSource soundFxs; public AudioClip jumpSound; public AudioClip crashSound; public Button restartButton; public TextMeshProUGUI gameOverText; private float jumpForce = 650f; private float gravityModifier = 1.5f; public bool isOnGround = true; public bool gameOver = false;
// Start is called before the first frame update
void Start()
{
playerRb = GetComponent<Rigidbody>();
playerJumpAnimation = GetComponent<Animator>();
soundFxs = GetComponent<AudioSource>();
Physics.gravity *= gravityModifier;
groundDirt.gameObject.SetActive(true);
}
// Update is called once per frame
void FixedUpdate()
{
if (Input.GetKeyDown(KeyCode.Space) && isOnGround && gameOver == false)
{
playerRb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
isOnGround = false;
playerJumpAnimation.SetTrigger("Jump_trig");
groundDirt.Stop();
soundFxs.PlayOneShot(jumpSound, 1.0f);
}
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.CompareTag("Ground"))
{
isOnGround = true;
groundDirt.Play();
}
else if(collision.gameObject.CompareTag("Obstacle"))
{
explosion.Play();
groundDirt.Stop();
playerJumpAnimation.SetBool("Death_b", true);
playerJumpAnimation.SetInteger("DeathType_int", 1);
gameOver = true;
restartButton.gameObject.SetActive(true);
gameOverText.gameObject.SetActive(true);
Debug.Log("Game Over");
//soundFxs.PlayOneShot(crashSound, 1.0f);
}
}
}
It looks like your issue is in the start method.
Physics.gravity *= gravityModifier;
Try setting that to
Physics.gravity = gravityModifier;
With it being *= gravity will slowly increase by 1.5 every time Start runs. Start only runs once and that is when the scene is loaded and the PlayerController is enabled. Explains why it is increasing every scene load.
The higher the gravity the less impact AddForce will have.
If you want to know more about how AddForce works, we have an article on our site.
https://www.monkeykidgc.com/2021/01/unity-rigidbody-addforce.html
Awesome! That seem to resolved the issue! The problem was indeed: Physics.gravity *= gravityModifier; I deleted the line and no more issue. I was not able to use your proposed line since gravityModifier is a float type variable. I adjusted the jump hight with my jumpForce variable and no issues! Thanlk you so much for you help!!
Your answer
Follow this Question
Related Questions
Increase Jump Through Mouse Clicks? 1 Answer
My code isn't working 3 Answers
How to make character controller jump? 1 Answer
Jumping with force in forward direction 2 Answers
ForceMode.Impulse Doesnt always fire. 2 Answers