0

So I am Somewhat new to coding (Started last year) so I might get a few things wrong or confused, I apologize preemptively.

I am trying to rotate my player in the same way the game series "Etrian Odyssey" does it, by slowly rotating it towards the desired cardinal direction. I have most of it working now, the player rotates left or right as desired, however, whenever it is looking "south", it is only able to rotate back to where it rotated from (I.E. if the player rotated from the left, it will only be able to rotate right until it goes back at least 1 time).

The following is all the code related to the rotation of the player (excluding the cardinal directions check because it's just brute forcing and enumerations that I don't feel like fixing for now)

This code handles the times where rotation happens and the rotation I want to reach

 else if (Input.GetKeyDown(KeyCode.LeftArrow))
            {
                desiredRot = transform.rotation * Quaternion.Euler(0,-90,0);
                StartCoroutine("RotateLeft");
            }
            else if (Input.GetKeyDown(KeyCode.RightArrow))
            {
                desiredRot = transform.rotation * Quaternion.Euler(0, 90, 0);
                StartCoroutine("RotateRight");
            }

This code handles the actual rotation, note that transform.rotate is desired for the effect of the player actually looking around, but it's not imperative, and I would rather it work than it look fancy

case Direction.left:
                transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
                break;
            case Direction.right:
                transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
                break;

This code handles when the rotation stops and when the player is able to move after selecting an input

IEnumerator RotateLeft()
    {
        isMoving = true;
        currentDirection = Direction.left;
        while (transform.rotation.y >= desiredRot.y)
        {
            yield return null;
        }
        currentDirection = Direction.stationary;
        isMoving = false;
    }

   IEnumerator RotateRight()
    {
        isMoving = true;
        currentDirection = Direction.right;
        while (transform.rotation.y <= desiredRot.y)
        {
            yield return null;
        }
        currentDirection = Direction.stationary;
        isMoving = false;
    }

Another issue is that no matter what I have tried, the desired rotation isn't perfect, as in, it's never a perfect 90 degree angle, which messes with the movement but honestly, I can worry about that later since movement is my priority, but thought it was worth mentioning in case that was the problem to begin with

I don't particularly remember what I have tried since I've been at this for at least a week, and honestly I'm just very tired at the moment and getting past my social anxiety just to ask this question lol

  • unity sometimes caclulates angles slightly weirdly so, to you and me, a circle is 360, but to unity sometimes its -180 -> 180 .. so ,lets say you have turned in one direction, and are at 180 .. you want to go more right to 270, but in its mind thats -90 .. so.. no sir, -90 is not >180 so it wont move.. Try addng some debugs throwing out the destination rotation and you might see this – BugFinder Jun 25 '23 at 09:43
  • Yeah that seems to be the issue, although I can't really figure out how to fix it either, i've even tried removin the rotate and setting the rotation of the item but I guess that wouldn't really solve the issue either. – JadeAutumn Jun 26 '23 at 01:38
  • So check if its negative and add 360? – BugFinder Jun 26 '23 at 07:27
  • That didn't seem to change anything sadly, I tried changing the rotation goal when the number is negative, or setting it to a specific number when it's getting locked, nothing really changes – JadeAutumn Jun 27 '23 at 02:59

0 Answers0