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

Question AddForce...

Discussion in 'Scripting' started by MarcinSok, Oct 3, 2022.

  1. MarcinSok

    MarcinSok

    Joined:
    Aug 3, 2022
    Posts:
    3
    Can somebody explain to me please how is it that if I AddForce upwards to a RigidBody on GetKey and hold the key down, the object goes up and then starts falling down? How is it that accumulated AddForce force wins over gravity and accelerates the object upwards but eventually loses? And then once it goes down enough the upwards force wins over the gravity again and the object goes up again but heigher than before and then it falls down again and then goes higher again than the previous time?

    Because it does not make sense to me at all.
    Any insight on this would be very appreciated.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    What's your code? When and how often are you calling AddForce?
     
    Bunny83 likes this.
  3. MarcinSok

    MarcinSok

    Joined:
    Aug 3, 2022
    Posts:
    3
    This is the code:

    void Start(){
    playerRb = gameObject.GetComponent<Rigidbody>();
    Physics.gravity *= gravityModifier;
    }

    void FixedUpdate(){
    if (Input.GetKey(KeyCode.Space) && !gameOver){
    playerRb.AddForce(Vector3.up * floatForce);
    }
    }

    Update - FixedUpdate makes no difference on the outcome.
    floatForce is set to 50;
    mass of the object is 1
    drag is 0
    angular drag is 0.05
    gravity is used
    iskinematic is false
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,571
    What is your "gravityModifier"? And is this script attached to more than one object? Because each object would multiply the global gravity. So this may be your issue. If the gravity is set to the default 9.81 m/s² and you multiply it by a value larger than 1, the gravity would get stronger with each object.

    I would highly recommend to not apply such relative changes to gravity. If you really need to do that, do it at a central location from a manager where you know that it's only called once. But even then you should not do a relative change like that. What if you reload your level? That would not reset the gravity setting of the physics system and your Start method would multiply it again.
     
    MarcinSok and StarManta like this.
  5. MarcinSok

    MarcinSok

    Joined:
    Aug 3, 2022
    Posts:
    3
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    How unfortunate... that really is a dreadful approach for Unity to have chosen.

    @MarcinSok You might want to move on to some better tutorial creators because there's a lot of issues with that particular Learn package.

    Personally I really like this guy... here's his intro video and he has a metric buttload of amazing tutorials:

    Imphenzia: How Did I Learn To Make Games:



    Here's his main "how to code," plus a few more creators too:

    Imphenzia / imphenzia - super-basic Unity tutorial:



    Jason Weimann:



    Brackeys super-basic Unity Tutorial series:



    Sebastian Lague Intro to Game Development with Unity and C#: