Jump height inconsistent after reload of the game
Hi there, I've noticed a strange issue in my game, it's an endless runner where you have to jump to avoid obstacles. If you collide with one, you die, a game over screen appears and you can either go back to main menu or restart the game.
However, if you restart the game, the character will not jump as high as before, same if you continue to die, each new game, the character jump will be lower and lower.
It only goes back to normal if you quit the game and restart entirely.
Here's the script for the player controller that handles the jump
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
//Rigibody
private Rigidbody playerRb;
//Référence à l'animator controller
private Animator playerAnim;
//Particule lorsque le joueur touche un obstacle
public ParticleSystem explosionParticle;
//Particule lorsque le joueur court
public ParticleSystem dirtParticle;
//Référence à l'audiosource du joueur
private AudioSource playerAudio;
//Audio lorsque le joueur saute
public AudioClip jumpSound;
//Audio lorsque le joueur touche un obstacle
public AudioClip crashSound;
//Force du saut
public float jumpForce = 10;
//Coefficient multiplicateur pour la gravité
public float gravityModifier = 1;
//Définie si le joueur est sur le sol ou pas
public bool isOnGround = true;
//Définie si le jeu est fini ou pas
public bool gameOver;
private bool canvasOnScreen = false;
public GameObject canvasObject;
// Start is called before the first frame update
void Start()
{
//Définie le rigidbody
playerRb = GetComponent<Rigidbody>();
//Définie l'animator
playerAnim = GetComponent<Animator>();
//Définie l'audio source du joueur
playerAudio = GetComponent<AudioSource>();
//Définie la gravité à utilisée
Physics.gravity *= gravityModifier;
}
// Update is called once per frame
void Update()
{
//SAUT : Si le joueur appuie sur la touche espace, faire sauter le personnage et définir qu'il est n'est plus au sol
if(Input.GetKeyDown(KeyCode.Space) && isOnGround && !gameOver)
{
playerRb.AddForce(Vector3.up*jumpForce,ForceMode.Impulse);
isOnGround = false;
playerAnim.SetTrigger("Jump_trig");
dirtParticle.Stop();
playerAudio.PlayOneShot(jumpSound, 1.0f);
}
if(gameOver==true)
{
dirtParticle.Stop();
}
}
private void OnCollisionEnter(Collision collision)
{
//Si le joueur entre en collision avec le sol, définir qu'il est au sol
if(collision.gameObject.CompareTag("Ground"))
{
isOnGround = true;
dirtParticle.Play();
}
//Si le joueur entre en collision avec un obstacle, définir le jeu comme étant fini.
else if(collision.gameObject.CompareTag("Obstacle"))
{
gameOver = true;
playerAnim.SetBool("Death_b", true);
playerAnim.SetInteger("DeathType_int", 1);
explosionParticle.Play();
dirtParticle.Stop();
playerAudio.PlayOneShot(crashSound, 1.0f);
ShowCanvas();
}
}
private void ShowCanvas()
{
if(gameOver && !canvasOnScreen)
{
canvasObject.GetComponent<ShowGameOver>().ShowCanvas();
canvasOnScreen = true;
}
}
}
And here is the script that handles the UI of the game over screen :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GOMenu : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void Restart()
{
SceneManager.LoadScene("Prototype 3");
}
public void MainMenu()
{
SceneManager.LoadScene("MainMenu");
}
}
Thanks for your inputs
Answer by jdg23 · Apr 06, 2021 at 05:47 PM
SOLVED
It was due to the gravity modifier, physics was not reset before loading the scene and so its new value from a first game was kept and multiplied at each reload.
Solved it by reseting the gravity before loading the scene
public void Restart()
{
Physics.gravity = new Vector3(0, -9.8f, 0);
SceneManager.LoadScene("Prototype 3");
}
public void MainMenu()
{
Physics.gravity = new Vector3(0, -9.8f, 0);
SceneManager.LoadScene("MainMenu");
}
Answer by tomjae3 · Jul 13, 2021 at 12:42 PM
Confirmed. I had an identical problem! This resolves all issues!
Thank you!
Answer by ab_raiyan · Sep 30, 2021 at 01:51 PM
Thanks a lot. This solution solved my issue too. But why the value of the y-axis is -9.8f when we are resetting the Physics.gravity?
Hi, I know this is old but maybe for anyone who finds this through google and is confused where that number comes from, it's the the default value set under Project Settings>> Physics >> Gravity
You can of course put whatever number you have for gravity if it's different :)
Your answer
