Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
2
Question by jdg23 · Apr 06, 2021 at 11:18 AM · bugjumpcharacter movement

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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");
 
     }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by tomjae3 · Jul 13, 2021 at 12:42 PM

Confirmed. I had an identical problem! This resolves all issues!

Thank you!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image navarone77 · Apr 19 at 09:32 PM 0
Share

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 >> Gravityalt text

You can of course put whatever number you have for gravity if it's different :)

screenshot-2022-04-19-173025.png (3.7 kB)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

181 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to jump in a 2D game 2 Answers

Help me in 2d Plataform game, walking in wall 0 Answers

How can I modify this to allow jumping with space? 0 Answers

Jumping with slight air control while retaining velocity 1 Answer

Jump OnCollision() Script not working 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges