Camera Rotation Based Hex Grid Movement

Hi,

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!

EDIT: Posted code twice

Yes, I do see that, on lines 3 and 4 above, so it certainly SEEMS like it should work.

Now is the time to debug! Find out what right and forward are! Find out how those values are being applied in your logic below. Here’s how:

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

You must find a way to get the information you need in order to reason about what the problem is.

PS: congrats on getting the code formatting correct at the top, but I think your entire post got text-cloned, with all the code appearing again below the properly-formatted stuff… :slight_smile:

Jesus, you are talking to me like I’m freshman junior, dude, I know how to debug, that’s why I asked help on the forum , because my attempts failed.

Oh good lord, please stop reading your own random emotions into a piece of boilerplate enginenering text that I’ve been posting on this forum for two years straight and which has helped COUNTLESS posters here, at least judging by how many people follow up with “Thanks that helped me figure it out!”

And yet you neglected to say “I’m moving it by the amount (1,0) and yet it appears to move by (0.7,0.7)…” and then asked yourself why.

Remember code is a trivial tiny irrelevant part of all this. Code does not even remotely matter quite honestly.

What matters is the data. And you said NOTHING about the quality of your data.

Ok, finally I’ve got it, I was using wrong axis, jesus, I need a vacation.

Hey, dude, @Kurt-Dekker , sorry for snapping at you

1 Like

it’s all good… I think I’ve used the wrong axis eight times in the past week alone, and one time wasn’t even related to programming.

The only point of all that baby step stuff is to break you out of the mind lock you might have… I like to call it “explain it to your dog,” where you call your dog over and explain to him, “Here I take the horizontal input and feed it into the vertical axis… oh crap, wait, that’s my problem.” and you give your dog a treat and get back to productive coding.