How to check which way the gravity is going

Hi, so I am making a game where you can change the gravity by pressing space, and I have made an arrow to show which way the gravity is going. This is the whole script:

using UnityEngine;

public class LevelTwo : MonoBehaviour
{
    enum GravityDirection { Down, Left, Up, Right };
    GravityDirection m_GravityDirection;

    public GameObject arrow;
    public Transform arrowTransform;

    void Start()
    {
        m_GravityDirection = GravityDirection.Down;
    }

    void Update()
    {
        if(m_GravityDirection == GravityDirection.Down)
        {
            arrowTransform.Rotate(0f, 0f, -180f);
        }
        if(m_GravityDirection == GravityDirection.Up)
        {
            arrowTransform.Rotate(0f, 0f, 0f);
        }
        if(m_GravityDirection == GravityDirection.Left)
        {
            arrowTransform.Rotate(0f, 0f, 90f);
        }
        if(m_GravityDirection == GravityDirection.Right)
        {
            arrowTransform.Rotate(0f, 0f, -90);
        }
    }

    void FixedUpdate()
    {
        switch (m_GravityDirection)
        {
            case GravityDirection.Down:
                Physics2D.gravity = new Vector2(0, -9.8f);
                if (Input.GetKeyDown(KeyCode.Space))
                {
                    m_GravityDirection = GravityDirection.Left;
                }
                break;

            case GravityDirection.Left:
                Physics2D.gravity = new Vector2(-9.8f, 0);
                if (Input.GetKeyDown(KeyCode.Space))
                {
                    m_GravityDirection = GravityDirection.Up;
                }
                break;

            case GravityDirection.Up:
                Physics2D.gravity = new Vector2(0, 9.8f);
                if (Input.GetKeyDown(KeyCode.Space))
                {
                    m_GravityDirection = GravityDirection.Right;
                }
                break;

            case GravityDirection.Right:
                Physics2D.gravity = new Vector2(9.8f, 0);
                if (Input.GetKeyDown(KeyCode.Space))
                {
                    m_GravityDirection = GravityDirection.Down;
                }
                break;
        }
    }
}

This does not work, the arrow goes crazy. I need the script in C#.

Please help

Thanks

1 Answer

1

You used arrowTransform.Rotate(…) during the update function, which means whatever vector you type inside of the “(…)” is constantly moving the arrowTransform every frame. If you want to set the arrow to a certain rotation simply use “arrowTransform.rotation = Quaternion.euler(…)” where the “…” is the same vector you used before hand.

What method would I put this in?