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