Home > Mobile >  How to have consistent rotation speed with transform.Rotate in unity?
How to have consistent rotation speed with transform.Rotate in unity?

Time:02-25

I am trying to create a "fnaf-esc" control where the player rotates around the Y axis based on the mouse's position relative to screen width. I haven't bothered with clamping the rotation yet, because I am facing an issue with the rotation speed slowing down when reaching the 180 degrees mark.

[SerializeField]
private GameObject Player;

// Mouse/Player Rotation Variables
float mouseX;
float mouseY;
Vector3 mousePos = new Vector3(0f, 0f, 0f);

[SerializeField]
private Transform playerBody;

[SerializeField]
private Camera cam;

void Update()
{
        mousePos = Input.mousePosition;

        if (mousePos.x <= (Screen.width * .4))
        {
            if (mousePos.x >= (Screen.width * .33))
            {
                Debug.Log("Turn left slowest");
                Player.transform.Rotate(0f, -0.05f, 0f);

            }
            else
            {
                if (mousePos.x < (Screen.width * .33) && mousePos.x > (Screen.width *.25))
                {
                    Debug.Log("Turn left slower");
                    Player.transform.Rotate(0f, -0.1f, 0f);
                }
                else if (mousePos.x <= (Screen.width * .25) && mousePos.x > (Screen.width * .2))
                {
                    Debug.Log("Turn left");
                    Player.transform.Rotate(0f, -0.15f, 0f);
                }
                else if (mousePos.x <= (Screen.width * .2))
                {
                    Debug.Log("Turn left");
                    Player.transform.Rotate(0f, -0.2f, 0f);
                }
            }
        }

        if (mousePos.x >= (Screen.width * .6))
        {
            if (mousePos.x <= (Screen.width * .66))
            {
                Debug.Log("Turn right slowest");
                Player.transform.Rotate(0f, 0.05f, 0f);
            }
            else
            {
                if (mousePos.x > (Screen.width * .66) && mousePos.x < (Screen.width * .75))
                {
                    Debug.Log("Turn right slower");
                    Player.transform.Rotate(0f, 0.1f, 0f);
                }
                else if (mousePos.x >= (Screen.width * .75) && mousePos.x < (Screen.width * .8))
                {
                    Debug.Log("Turn right");
                    Player.transform.Rotate(0f, 0.15f, 0f);
                }
                else if (mousePos.x >= (Screen.width * .8))
                {
                    Debug.Log("Turn right");
                    Player.transform.Rotate(0f, 0.2f, 0f);
                }
            }
        }
    }

}

When testing, I just kept my mouse in the same position and the rotation was still slowing around 180.

I tried searching for solutions and found that "linear interpolation" is the reason for this, though I can't find any solutions for a consistent turn speed.

  • How can I achieve a turn speed that doesn't slow around the 180 degrees mark?
  • Is transform.Rotate the best method to achieve this?

CodePudding user response:

It's better to use RotateArround , take a look at the documentation : docs.unity3d.com/ScriptReference/Transform.RotateAround.html

  • Related