Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by fer_franco · Feb 28, 2021 at 08:56 AM · jumpforce

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?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 3 · 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 fer_franco · Mar 01, 2021 at 02:04 AM 0
Share

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

   

}

avatar image mrmatt1877 fer_franco · Mar 01, 2021 at 03:06 AM 0
Share

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

avatar image fer_franco mrmatt1877 · Mar 01, 2021 at 03:30 AM 0
Share

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

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

114 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

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


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