Camera Rotation Based Hex Grid Movement

Viewed 17

I have a hex grid and I have the following code to determine the new hex position based on input:

    float horizontal = InputManager.Instance.PlayerControl.PanCursor.X;
    float vertical = InputManager.Instance.PlayerControl.PanCursor.Y;
    var right = _camera.transform.right * horizontal;
    var forward = _camera.transform.forward * vertical;
    var cameraAdjustedMovement = right + forward;

    if (cameraAdjustedMovement.x < -Threshold)
    {
        if (cameraAdjustedMovement.y > Threshold)
        {
            movePosition = ScenarioManager.EAdjacentPosition.ETopLeft;
        }
        else if (cameraAdjustedMovement.y < -Threshold)
        {
            movePosition = ScenarioManager.EAdjacentPosition.EBottomLeft;
        }
        else
        {
            movePosition = ScenarioManager.EAdjacentPosition.ELeft;
        }
    }
    else if (cameraAdjustedMovement.x > Threshold)
    {
        if (cameraAdjustedMovement.y > Threshold)
        {
            movePosition = ScenarioManager.EAdjacentPosition.ETopRight;
        }
        else if (cameraAdjustedMovement.y < -Threshold)
        {
            movePosition = ScenarioManager.EAdjacentPosition.EBottomRight;
        }
        else
        {
            movePosition = ScenarioManager.EAdjacentPosition.ERight;
        }
    }

The problem is than this works only when camera's Y rotation is 180. When it's 0, only horizontal movement works, while vertical is inverted. On other angles it's just broken.

Why? I take into account camera's forward and right, but it doesn't make sense.

How can I achieve movement on hex grid while taking into account camera's rotation, so when I press left - it goes CAMERA'S left.

Thanks!

0 Answers
Related