Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Bug Please help going insane

Discussion in 'Editor & General Support' started by iceasson, Sep 28, 2022.

  1. iceasson

    iceasson

    Joined:
    Mar 7, 2022
    Posts:
    15
    this is from Unity Learn Challenge 3 - Balloons, Bombs, & Booleans



    after getting this weird issue

    i deleted everything and started just with the balloon and set its force value to 10 let it fall on the ground level and it would go up slowly.

    after changing an object position i.e camera or background.

    i press play, let the balloon fall to the ground level and the balloon force is CRAZY DIFFERENT even though its value is at 10.


    the script is just a simple addforce script with nothing else on it


    Not sure why but i cant play the video in this forum please visit the youtube site if you have the same problem youtube.com/watch?v=N57x9bjI3_8
     
    Last edited: Sep 28, 2022
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,917
    Show the script that affects the balloon.
     
  3. iceasson

    iceasson

    Joined:
    Mar 7, 2022
    Posts:
    15
    Code (CSharp):
    1. public class PlayerController : MonoBehaviour
    2. {
    3.     private Rigidbody playerRb;
    4.     public float floatForce ;
    5.  
    6.     void Start()
    7.     {
    8.         playerRb = GetComponent<Rigidbody>();
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.         if (Input.GetKey(KeyCode.Space))
    15.         {
    16.             playerRb.AddForce(Vector3.up*floatForce, ForceMode.Force);
    17.         }
    18.     }
    19. }
     
    Last edited: Sep 28, 2022
  4. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,415
    Maybe use GetKeyDown. Now you will add force every frame as long as you hold the spacebar

    Also use code tags if you post code
     
  5. iceasson

    iceasson

    Joined:
    Mar 7, 2022
    Posts:
    15
    getkeydown was my first attempt writing the code,so i tried different things like getkey and left it there since it was all the same and giving me that weird issue
     
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,996
    try adding forces inside FixedUpdate
     
    iceasson likes this.
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Definitely this.

    If you do an
    AddForce()
    with
    -Physics.gravity * mass
    it will only hover perfectly if that code is inside of FixedUpdate()

    Otherwise there will be variation.
     
    iceasson likes this.
  8. iceasson

    iceasson

    Joined:
    Mar 7, 2022
    Posts:
    15
    Thanks! this was true, however the challenge itself never thought us to put physics related code into fixedupdate but their project did not have the above issue putting the code in normal update. so its weird,especially for a beginner that doesn't know about this.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    These discussions might help you cement why exactly:

    Two good discussions on Update() vs FixedUpdate() timing:

    https://jacksondunstan.com/articles/4824

    https://johnaustin.io/articles/2019/fix-your-unity-timestep