- Home /
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.
Rich
Answer by homeros · May 27, 2011 at 12:21 PM
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.
That could work... I'm not sure I like it, but if all else fails!
Of course, if they're clicking that close to the character to start with, they're not likely to be going to the edge of the screen anyway.
of course, it would also mean a large change in camera viewpoint once they released the button as well... which would be jarring to the player. Hmmm, I keep changing my $$anonymous$$d on this solution...
Answer by demize2010 · May 27, 2011 at 12:43 PM
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 -
http://unity3d.com/support/documentation/ScriptReference/Input.GetMouseButton.html
The rotation is only happening when the mouse is pressed, using Get$$anonymous$$ouseButton. I think you're missing the point slightly. This isn't a code problem with the existing code, it's more a theoretical problem around what else I can implement to stop it from behaving the way it "should" in this instance.
Answer by Rich 1 · May 31, 2011 at 08:17 AM
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 :P
Thanks for your inputs people.
Your answer
Follow this Question
Related Questions
How to disable click to move on interface elements ? 0 Answers
mouse clicking on the plane and drag to other position 1 Answer
Move object towards mouse but if mouse moves object fallows it's original path 0 Answers
How can I make player move with cursor?,How can I make player move with mouse? 1 Answer
Prevents a mouse click on interface to trigger a click to move script 1 Answer