[SOLVED] Custom mouse position origin

Hi,
I’m currently making a twin-stick shooter and I got some troubles with mouse position input.
I need to get the direction between my player, at the center of the screen, and my cursor. The problem is mouse position’s origin (0,0) is not the center of the screen but the bottom left corner.
There is a way to set a custom origin ? Ideally on player’s position.

1 Like

Couldn’t you simply offset the value of mousePosition ?

It’s a good idea, i tried something and it almost works as intended, here a gif of the result.

As you can see the little ball, which is my aim object, has a strange behavior.
There must be an error in my code :

mousePosition -= (Vector2)Camera.main.WorldToScreenPoint(new Vector2(transform.position.x,transform.position.z));
mousePosition.Normalize();
aimGuide.position = transform.position + new Vector3(mousePosition.x, -.5f, mousePosition.y);

If you have an idea how to do it I take it ^^

Bump

Okay, I found someone with an answser : Top down aim at mouse - Questions & Answers - Unity Discussions
I personally don’t use the rotation thing since I only want a direction, so I simply do this :

Ray ray = Camera.main.ScreenPointToRay(mousePosition);
Plane plane = new Plane(Vector3.up, Vector3.zero);
float distance;
if (plane.Raycast(ray, out distance))
                {
                    Vector3 target = ray.GetPoint(distance);
                    aimDirectionMouse = target - transform.position;
                    aimDirectionMouse.Normalize();
                    aimGuide.position = transform.position + new Vector3(aimDirectionMouse.x, -.5f, aimDirectionMouse.z);
                }

Yes, but the issue with that is that you’re not going to have it nicely bound into one combined action. Ideally, I one would have a single action that is hooked up to both the mouse and joystick if you are going for gamepad support, which unfortunately I don’t think is supported by the new Input System yet.

I think you can’t have only one piece of code that handle joystick input and mouse position, but I may be wrong ^^

1 Like