Click-to-move problems, mouse cursor moving

Hi all,

I just typed a nice long question, then logged in, and it’s not here any more. So here we go again…

I have spent two days crawling through this site, the Unity Reference, and the wiki, and can’t find anything that will help me out of this bind, so any answers, whether theoretical or practical implementation-based answers will help. I am fine with a Windows-only solution if that helps. I am quite tied to GUI functions (mouse hovers, gui buttons etc) so I would like to use the actual mouse position/cursor, not replace it with my own. If that is the only solution to this, any tips on how to easily convert my existing GUI stuff would be appreciated too.

So, the problem is this: I am implementing a click-to-move script, based on the one in the wiki. It’s working fine so far.

My camera is not centered on my Player Character, but on an empty GameObject, childed to the PC, about 1/4 of a screen-height away from him. All the logic around camera movement is correct so far, moving and turning my character with the keyboard works as I expect it to. (The point of this was to have more visible screen-space in front of the character, rather than behind him).

Combining these two working scripts however, gives me this problem:

If I click behind the PC, within the distance of the CameraTarget, the first thing that happens (and this is correct) is that the PC turns to face the clicked point. Then the camera moves to center on the new position of the CameraTarget. This is also correct. But now the PC is the other side of the centre of the screen, with his back to where the mouse pointer is. So he turns to face the mouse pointer… etc etc, and we get a flipping effect as he spins forever on the spot.

I know I could fix this by using MouseButtonUp instead of MouseButton, but I want the behaviour where you click and hold to have the character walk towards the mouse pointer as the mouse pointer keeps getting pushed back, and the character keeps walking in that direction until you let go.

My first thought was to move the mouse cursor after the character had turned, to the same place in game-space as it was when you clicked, but Unity won’t let me move the cursor.

My second thought was to disable the click event inside that radius, but that’s my combat zone (and then some) - so this won’t work. I suppose I could disable click-and-hold in that range, but not sure the user experience would be that great with inconsistent behaviour.

Any advice, with or withour implementation, gratefully accepted.


Locking the camera movement while the mouse is pressed might be a solution. So when the character moves to the edge of the screen, user will have to stop pressing.

If you post the code that is making the char turn it would be helpful, my suspicion is that you need to move the rotation code to only run while the mouse button is pressed?

For example using GetMouseButton -

What I’ve ended up doing is disabling the “Turn” function if the click-point is inside the radius of the camera target. So if you click just behind the character, he will back up to that point, rather than turn. If you click further away, the character turns and walks. It feels pretty natural to use (to me) - I just need to get the animations right now. Fun with dot products :stuck_out_tongue:

Thanks for your inputs people.