Logo my studio is game, portfólio is developer game

Set a rotation to an object in Unity

In this post, I show you how to set a rotation to an object in Unity. In the example, I made a typical spin to the top-down SNES games.

using UnityEngine;

public class QuaternionEuler : MonoBehaviour
{
    #region Variables
    private float buttonSentivity = 0.2f;
    [HideInInspector] public bool dirRight;
    [HideInInspector] public bool dirLeft;
    [HideInInspector] public bool dirForward;
    [HideInInspector] public bool dirBack;
    #endregion

    #region Methds in Unity
    private void Update()
    {
        MovePlayer();
    }
    #endregion

    #region Custom methds
    private void MovePlayer()
    {
            if (Input.GetAxis("Horizontal") > buttonSentivity)
            {
                SetDirArrow(0, 90, 0);
                DirFalse();
                dirRight = true;
            }
            else if (Input.GetAxisRaw("Horizontal") < -buttonSentivity)
            {
                SetDirArrow(0, 270, 0);
                DirFalse();
                dirLeft = true;
            }
            else if (Input.GetAxisRaw("Vertical") > buttonSentivity)
            {
                SetDirArrow(0, 0, 0);
                DirFalse();
                dirForward = true;
            }
            else if (Input.GetAxisRaw("Vertical") < -buttonSentivity)
            {
                SetDirArrow(0, 180, 0);
                DirFalse();
                dirBack = true;
            }
    }

    private void SetDirArrow(float x, float y, float z)
    {
        Vector3 rotationDir = new Vector3(x, y, z);
        transform.rotation = Quaternion.Euler(rotationDir);
    }

    private void DirFalse()
    {
        dirRight = false;
        dirLeft = false;
        dirForward = false;
        dirBack = false;
    }
    #endregion

}

 

Make contact!

Did you like this content? Let's talk...

I'm a game developer, I use unity. If you want to develop something, tell me!