Rotation difficulty

public class PlayerControler : MonoBehaviour
{
    public GameObject MoveToPosion;

    private Vector3 newpos;
    private Quaternion LookDirForwrd;
    private Quaternion LookDirBack;
    private Quaternion LookDirRight;
    private Quaternion LookDirLaft;



    private void Start()
    {
        LookDirForwrd = Quaternion.Euler(0, 0, 0);
        LookDirBack = Quaternion.Euler(0, 180, 0);
        LookDirRight = Quaternion.Euler(0, 90, 0);
        LookDirLaft = Quaternion.Euler(0, -90, 0);  
    }

    private void Update()
    {
        

        if (Input.GetKeyDown(KeyCode.W) && gameObject.transform.rotation == LookDirForwrd) // Move Forwrd
        {
            newpos = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z + 3);
            GameObject ghost = Instantiate(MoveToPosion, newpos, gameObject.transform.rotation);

            DoTask(ghost);
        }
        else if (Input.GetKeyDown(KeyCode.W) && gameObject.transform.rotation == LookDirRight)
        {
            newpos = new Vector3(gameObject.transform.position.x + 3, gameObject.transform.position.y, gameObject.transform.position.z);
            GameObject ghost = Instantiate(MoveToPosion, newpos, gameObject.transform.rotation);

            DoTask(ghost);

        }
        else if (Input.GetKeyDown(KeyCode.W) && gameObject.transform.rotation == LookDirLaft)
        {
            newpos = new Vector3(gameObject.transform.position.x - 3, gameObject.transform.position.y, gameObject.transform.position.z);
            GameObject ghost = Instantiate(MoveToPosion, newpos, gameObject.transform.rotation);

            DoTask(ghost);

        }
        else if (Input.GetKeyDown(KeyCode.W) && gameObject.transform.rotation == LookDirBack)
        {
            newpos = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z -3 );
            GameObject ghost = Instantiate(MoveToPosion, newpos, gameObject.transform.rotation);

            DoTask(ghost);

        }


        if ((Input.GetKeyDown(KeyCode.S)) && gameObject.transform.rotation == LookDirForwrd)// Move Back
        {
            newpos = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z - 3);
            GameObject ghost = Instantiate(MoveToPosion, newpos, gameObject.transform.rotation);

            DoTask(ghost);
        }
        else if (Input.GetKeyDown(KeyCode.S) && gameObject.transform.rotation == LookDirLaft)
        {
            newpos = new Vector3(gameObject.transform.position.x + 3, gameObject.transform.position.y, gameObject.transform.position.z);
            GameObject ghost = Instantiate(MoveToPosion, newpos, gameObject.transform.rotation);

            DoTask(ghost);
        }
        else if (Input.GetKeyDown(KeyCode.S) && gameObject.transform.rotation == LookDirRight)
        {
            newpos = new Vector3(gameObject.transform.position.x - 3, gameObject.transform.position.y, gameObject.transform.position.z);
            GameObject ghost = Instantiate(MoveToPosion, newpos, gameObject.transform.rotation);

            DoTask(ghost);
        }
        else if (Input.GetKeyDown(KeyCode.S) && gameObject.transform.rotation == LookDirBack)
        {
            newpos = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z +3 );
            GameObject ghost = Instantiate(MoveToPosion, newpos, gameObject.transform.rotation);

            DoTask(ghost);
        }

        if (Input.GetKeyDown(KeyCode.A))// turn left
        {
            gameObject.transform.Rotate(0, -90, 0);
        }

        if (Input.GetKeyDown(KeyCode.D))// turn rghit
        {
            gameObject.transform.Rotate(0, 90, 0);
        }
    }

    private void DoTask(GameObject ghost)
    {
        if (ghost.GetComponent<CapsuleCollider>().CompareTag("Wall"))
        {
            Debug.Log("cant move");
            Destroy(ghost);
        }
        else
        {
            gameObject.transform.position = ghost.transform.position;
            Destroy(ghost);
        }

      
    }`Preformatted text`

      
    }`Preformatted text`

I am trying to make a movement system like in “inscryption” when you can move in the cabin in act 1.
now I am new to euler and quaternion and I have a problem that after the player dose a 270 turn the code is not working until he dose another 270.

Just to explain the rest of the code:
I make a collider to check if there is anything in the way before I move the player.

This is my first poste so I would love to know if next time I need to poste my question in any other way/format

Hi, if I understand correctly what you want to achieve, the following code should work. I tested it and it works for me. I think the problem with your code is checking rotation in the update method.

using UnityEngine;

public class PlayerControler : MonoBehaviour
{
    public GameObject MoveToPosition;
    private Vector3 newpos;
    private Quaternion[] lookDirections = new Quaternion[4];

    private void Start()
    {
        lookDirections[0] = Quaternion.Euler(0, 0, 0); // Forward
        lookDirections[1] = Quaternion.Euler(0, 90, 0); // Right
        lookDirections[2] = Quaternion.Euler(0, 180, 0); // Back
        lookDirections[3] = Quaternion.Euler(0, -90, 0); // Left
    }

    private void Update()
    {
        // Get the current direction
        int currentDirection = GetDirection();

        // Move forward
        if (Input.GetKeyDown(KeyCode.W))
        {
            newpos = GetNewPosition(currentDirection, 3);
            GameObject ghost = Instantiate(MoveToPosition, newpos, transform.rotation);
            DoTask(ghost);
        }

        // Move backward
        if (Input.GetKeyDown(KeyCode.S))
        {
            newpos = GetNewPosition(currentDirection, -3);
            GameObject ghost = Instantiate(MoveToPosition, newpos, transform.rotation);
            DoTask(ghost);
        }

        // Turn left
        if (Input.GetKeyDown(KeyCode.A))
        {
            transform.Rotate(0, -90, 0);
        }

        // Turn right
        if (Input.GetKeyDown(KeyCode.D))
        {
            transform.Rotate(0, 90, 0);
        }
    }

    private int GetDirection()
    {
        // Get the current direction
        for (int i = 0; i < lookDirections.Length; i++)
        {
            if (Quaternion.Angle(transform.rotation, lookDirections[i]) < 45f)
            {
                return i;
            }
        }
        return 0; // Default to forward
    }

    private Vector3 GetNewPosition(int direction, float distance)
    {
        // Calculate the new position based on the direction
        switch (direction)
        {
            case 0: // Forward
                return new Vector3(transform.position.x, transform.position.y, transform.position.z + distance);
            case 1: // Right
                return new Vector3(transform.position.x + distance, transform.position.y, transform.position.z);
            case 2: // Back
                return new Vector3(transform.position.x, transform.position.y, transform.position.z - distance);
            case 3: // Left
                return new Vector3(transform.position.x - distance, transform.position.y, transform.position.z);
            default:
                return transform.position;
        }
    }

    private void DoTask(GameObject ghost)
    {
        if (ghost.GetComponent<CapsuleCollider>().CompareTag("Wall"))
        {
            Debug.Log("Can't move");
            Destroy(ghost);
        }
        else
        {
            transform.position = ghost.transform.position;
            Destroy(ghost);
        }
    }
}
1 Like

Wow!
first of all thank you for the fast response! its works exactly as I wanted it to.

May I ask you what was your thought proses when you saw my code in regards of how to fix it?
I aspire to be a game developer as a job, so Would love to understand and not just have the solution be given to me.

Hello, I’m glad I could help.
First I saw your update function with so many if/else if statements. There is almost always an easier or shorter way to check or retrieve things. First you can always look at what you “mainly” need to check. Here it was the input with “w,a,s,d”. You can see that they require different criteria to be true, but each of the statements checked the same variable. Or at least you can get it to check only one variable by putting the same type in an array or list and checking the different types in that array/list. Here all you needed was the current direction, which I first checked in the update function and then used in the various statements. Most of the time you can call a function in the instructions and then check it further with a switch statement, for example. This gives you a better overview of everything and makes it easier to check for errors. Plus, most of the time it’s much shorter.
There are probably a few more little things that could be optimized over time, etc.
I hope this helps. If you have any further questions, please feel free to message me :slight_smile:

1 Like