Sebastian_R72
Sebastian_R72

Reputation: 21

Unity - Gravity not working when opening from another scene

I am creating a simple mini-game in unity as a separate scene. The problem is that the gravity works completely fine on my rigidbody when I start from the mini-game scene, but when I move to the mini-game scene from another the gravity stops working and my player can only go up.

Here is what I've tried:

I do have a GameController object which is carried between the scenes, but none of it's scripts should affect the scene (I can provide the OnLevelWasLoaded() parts of the scripts or any other parts).

Here is the code in my player controller:

void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        box = GetComponent<BoxCollider2D>();
        _renderer = GetComponent<SpriteRenderer>();
        velVer = 30f;
        velHor = 10f;
    }

void Update()
    {
        moveHor = Input.GetAxisRaw("Horizontal");
        moveVer = Input.GetAxis("Vertical");
        buttonUp = Input.GetButtonUp("Vertical");
        _moveVector = new Vector2(moveHor, moveVer);
    }

    void FixedUpdate()
    {

        if (IsGrounded() && buttonUp)
            rb.velocity = new Vector2(velHor * _moveVector.x, velVer * _moveVector.y);
        if (_moveVector.x != 0 && _moveVector.y == 0)
            rb.velocity = new Vector2(velHor * _moveVector.x, rb.velocity.y);
    }

    private bool IsGrounded()
    {
        RaycastHit2D raycast = Physics2D.BoxCast(box.bounds.center, box.bounds.size, 0f, Vector2.down, 0.1f, layer);
        return raycast.collider != null;
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag.Equals("Heart"))
        {
            other.gameObject.SetActive(false);
        }
    }

I have commented out any references to the GameController object from all scripts in the mini-game scene, so the GameController should not have any impact on my mini-game character controller.

I am desperate, I have searched online for more than 3 hours and haven't found anything that could help me (if I did actually miss a solution my apologies). I can provide any further information you may require.

EDIT: Here are Gifs of character behaviour when I start the game from the mini-game scene, and when I move to the mini-game scene from another.

Working (started mini-game directly):

enter image description here

Not Working (moved to mini-game from another scene):

enter image description here

Upvotes: 2

Views: 1889

Answers (2)

Sebastian_R72
Sebastian_R72

Reputation: 21

Okay I have found the issue! I thought the problem would lie inside the scripts that are inside of my mini-game scene, or in the scripts carried into the scene by my GameController, but that was not the case. The problem laid in a script attached to an object located in a scene from which I moved to the mini-game.

Here if the troublesome line:

Physics2D.gravity = Vector2.zero;

Because my game is 2D with Axonometric projection (3/4) I set the gravity to Vector2.zero very early into the development. I have replaced that with simply setting the gravity scale to 0.

rb.gravityScale = 0;

Even though the script with the troublesome line doesn't move to my mini-game scene it modified the gravity setting of the entire project. VERY silly mistake, which I probably would find, but after many hours of constant development my brain wasn't functioning too well, and I decided to post this question.

Thanks for the help anyway guys!

Upvotes: 0

MohammadReza Arashiyan
MohammadReza Arashiyan

Reputation: 343


Check for three things:
1) Are you sure that you didn't set timeScale to 0 in any other script?
2) Does your player have the same scale on both levels?
3) Why do you use Physics2D.BoxCast in IsGrounded function? I usually use Raycast2D for the ground checking process.

Upvotes: 0

Related Questions