I’m currently using
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
to track my mouse position. I’m trying to get the right stick on my gamepad to also move the mouse cursor.
I tried multiple approaches but haven’t been able to.
My last attempt was:
I’ve tried creating 2 new axis in my input manager, named them Mouse X and Mouse Y, and defined them as Joystick axis along the 4th and 5th axis.
I also tried to get these outputs by using
Vector2 mousePosVector = new Vector2 (Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
This is kinda where I’m stuck
“haven’t been able to” isn’t a super-useful diagnosis.
How to report your problem productively in the Unity3D forums:
http://plbm.com/?p=220
Pay attention to the meaning of the .z component of what you pass into ScreenToWorldPoint()… it is important and may have everything to do with your error. Check the docs for what I mean. It’s a little sentence towards the end of the last paragraph.
Cool, thanks for the reference, I like the 4 step approach.
So let me rephrase my question, at the moment I am using
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
to get my mouse position from the screen into my world, while using the mouse, this is working flawlessly.
I’m working on duplicating this on an Xbox controller. I don’t know how to define this ability to get the mouse location to something that functions with a the right stick of the controller, basically, when moving the right stick on my controller, I am able to have my character follow to that location, just as they do at the moment with a mouse cursor.
So my question is, how do I mirror that input from my mouse to my right stick on the controller. I just realized this might not be a strictly scripting question, the solution might be from the editor, but my initial approach was to get this through code.
My approach to solving this was to define a vector2 that pulls the Mouse X and Mouse Y positions and passing them as inputs in the ScreenToWorldPoint() function, but I am not getting any feedback that this is working and the character isn’t moving.
Well in that case you must find a way to get the information you need in order to reason about what the problem is.
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
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.
You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene
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.
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 can’t move the actual mouse with Unity’s API. You can set the position of the mouse cursor with some platform specific APIs, like the Windows one, but they obviously won’t work on all platforms. Your next bet is to use what’s called a software cursor, where you manually move a sprite around the screen, but that comes with its own caveats like not interacting with the UI out of the box without writing your own raycaster and not being perfectly in-sync with the actual mouse during rendering because of the nature of graphics hardware.
Pick your poison.