Search Unity

Question Turning Gravity On and Off?

Discussion in 'Physics' started by bellehuberty, Jul 7, 2023.

  1. bellehuberty

    bellehuberty

    Joined:
    Mar 26, 2023
    Posts:
    2
    Hello! I'm working on a game for a game jam, and need some help. It's set in space, and I'd like a big part of the game to be leaving your spaceship, going into zero-gravity, and collecting space junk. However, I'm having some trouble getting gravity to turn on and off upon entry and exit to the spaceship. I'm trying to make use of the Rigidbody.useGravity function with the following code, which is applied to the spaceship.

    -------------------- Code Start ---------------------------------------

    public Collider coll;
    public bool useGravity;

    void Start()
    {
    coll = GetComponent<Collider>();

    }

    // Enables gravity on all rigidbodies entering this collider.
    void OnTriggerEnter(Collider other)
    {
    if (other.attachedRigidbody)
    other.attachedRigidbody.useGravity = true;
    }
    // Disables gravity on all rigidbodies entering this collider
    private void OnTriggerExit(Collider other)
    {
    if(other.attachedRigidbody)
    other.attachedRigidbody.useGravity = false;
    }

    ------------------ Code End -------------------------------------------------

    However, when I'm in play mode and moving my character around, the "Use Gravity" toggle in the Rigidbody component is not checking on and off as I exit and enter the spaceship's collider. Any ideas of what I've done wrong? Thank you!
     
  2. bellehuberty

    bellehuberty

    Joined:
    Mar 26, 2023
    Posts:
    2
    Solved...... I forgot to set coll.isTrigger to true. I wish there was a way to delete threads so I could hide my shame....