Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Rich 1 · May 27, 2011 at 12:01 PM · mousemovecursorclick

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rich 1 · May 27, 2011 at 12:28 PM 0
Share

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.

avatar image Rich 1 · May 27, 2011 at 01:01 PM 0
Share

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...

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rich 1 · May 27, 2011 at 12:49 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges