Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 SSwift · Mar 02, 2013 at 03:15 AM · mouseraycastingmouse control

Click Mouse anywhere for different actions

Hi there,

Disclaimer. I'm VERY new at this. I've been in game dev many years ago as a character animator, so I know the pipelines and workflows of game dev inside out, but, have never sat and made my own. SO... My very first, super newby question:

I want to be able to click the mouse anywhere on the screen and, depending on what I click on, the character will either walk to the object, a message may appear, an internal function may trigger, etc etc.

What would be the best way to create a base system for this? IE: find out what is clicked is one thing (raycasting I assume). Then, based on what is clicked, perform the task.

Any feedback would be greatly appreciated.

Thank you.

Rob

Comment
Add comment · Show 2
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 Chronos-L · Mar 02, 2013 at 03:36 AM 0
Share

It seems like you are on the right track. A raycast test will be able to help you to get what object you are clicking on. Then, based on the gameObject tags, you can use either switch or if-else statements to run different actions.

avatar image robertbu · Mar 02, 2013 at 04:12 AM 0
Share

One thing that stands out with your question is the multiple different types of objects. You have a few different ways you can deal with them if you raycast. First you can do as @Chronos suggests and put the knowledge of the different objects in the code that is doing the raycasting. For example if the user clicking on a bomb the raycaster deter$$anonymous$$es it is a bomb and call the Explode() method of the bomb.

There are alternate methods that push that knowledge down to the object that you may want to take a look at:

  • Interfaces

  • Abstract Classes

Depending on your usage and platform, you might want to look at the On$$anonymous$$ouse* methods (On$$anonymous$$ouseDown(), On$$anonymous$$ouseUp()...). They make objects responsible for processing their own mouse/finger input.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SSwift · Mar 03, 2013 at 08:56 AM

First of all thank you both so much. It's really appreciated.

So, thinking out loud. I can basically give an object properties and, when clicked (Mac/PC first run for my sanity), I can determine if it's an object that has the ability to be utilised in my game via.. "tags". I'll look into tags, thank you.

So if it's an object that is say, a floor object, then the tag says "I'm a floor" so the character will walk to that spot. If it's a light switch. Then the tags will say "I'm a light switch" and the character can then interact with it.

Would that sound correct? Based on what you're saying. I'll take a look further into this. Again, much appreciated.

Rob

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 robertbu · Mar 03, 2013 at 09:06 AM 0
Share

Actually, that's the way @Chronos-L 's solution would work. With Interfaces and Abstract Classes, there would be a way for the Raycaster to call a common method no matter what the object. The Raycaster would never know the type of object or anything about its internals, just that it was a kind of object that had a common interface.

With On$$anonymous$$ouse* methods, the model is again different. Unity does the raycasting for you, so only the object knows about the raycast. So the the object can process the hits any way it likes, no Raycaster needed.

Beyond tags, you can also use scripts and GetComponent() to "tag" objects.

avatar image Chronos-L · Mar 04, 2013 at 12:34 AM 0
Share

To compile both @robertbu's and my answer, and just to give you a sneak peek on what you will be doing:

Raycast + tag

 public class Raycaster : $$anonymous$$onoBehaviour {
     public Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     public RaycastHit hit;
    
     void Update() {
         if (Physics.Raycast(ray, out hit, 100)) {
             GameObject go = hit.gameObject;
             
             if( go.CompareTag("Something") {
 
             }
             else if( ... ) {
 
             }
         }
         
     }
 }

Raycast + Interface/Abstract class

     public class Raycaster : $$anonymous$$onoBehaviour {
         public Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         public RaycastHit hit;
        
         void Update() {
             if (Physics.Raycast(ray, out hit, 100)) {
                Clickable click = hit.gameObject.GetComponent<Clickable>();

                if( click != null )
                   click.Process( ... );                
             }            
         }
     }

On$$anonymous$$ouse

 public class ClickObject : $$anonymous$$onoBehaviour {
     void On$$anonymous$$ouseDown() {
         //When this is clicked
     }
 
     void On$$anonymous$$ouseEnter() {
        //The mouse start `touching` this object
     }
 
     void On$$anonymous$$ouseExit() {
        //The mouse just exits this object
     }
 }

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me with my TPS Controller/Camera? 1 Answer

how to drag an object using 2d raycasts 0 Answers

Disable/enable script and animation when you move your mouse cursor 1 Answer

Return BoxCollider Rect values in screen coordinates 0 Answers

Raycast from secondary camera 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