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 lvictorino · May 21, 2014 at 12:47 PM · inputclickinputmanagermanagerstrategy

Click strategy

Hello,

I work with Unity for a long time now, and I there is still a concept that bothers me: the click strategy.

What strategy do you use when it comes about player interactions?

  • When player clicks on the scene do you have a high entity that will receive the input, do the raycast to check on what part of the screen the player has clicked, and throw the click information to a lower component according to the clicked object ?

  • When player clicks on the scene do you make all your "clickable" entities check if the click was for them (that means a raycast per clickable entity) ?

  • When player clicks on the scene do you use magic?

  • When player clicks on the scene do you treat UI and interactive game object differently? How?

My problem is either to have to many raycast made or not being fully able to handle specific situations like non-interactive objects when the game is supposed to be paused...

I can't find any documentation about what strategy is the best so I wonder how you do that kind of click management logic. Thanks for your help.

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 DMGregory · May 21, 2014 at 01:09 PM 0
Share

Personally, I use magic.

avatar image Vunpac · Jun 21, 2014 at 01:50 AM 0
Share

@D$$anonymous$$G rofl now when you say magic, are you speaking of an existing input management system called magic? Or just flat out magic?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Romano · May 21, 2014 at 05:03 PM

Here's a simplified version of the current method I'm using for my game (which is a 2D point and click game written in javascript):

Click Script + Input Manager

I have an empty game object with 2 scripts attached, ClickScript and InputManager.

Click script basically sends a raycast out from the main camera and when it hits a box collider it sends the raycast info to the input manager like this:

 if (Input.GetMouseButton(0))
 {
   // A function that does one raycast and returns a raycast hit.
   raycastHit = GetRaycast(); 

   // Call the function OnLeftClick on the inputManager and send the raycastHit as a parameter
   inputManager.OnLeftClick(raycastHit);
 }

And in the InputManager:

 function OnLeftClick(raycastHit : RaycastHit2D)
 {
   // If you've clicked a person...
   if (raycastHit.collider.CompareTag("Person"))
     {
       TalkToPerson();
     }
   // If you've clicked an exit...
   else if (raycastHit.collider.CompareTag("Exit"))
     {
       Exit();
     }
 }

And so the tag of the game object you click on determines what happens. It took me ages to work out this method and I've found that it is very flexible and very quick to work with and to add new features into the game.

If its a 2D game youre making I can direct you to a couple of handy sites/ scripts for Unity.

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 lvictorino · Jun 20, 2014 at 12:01 PM 0
Share

Thanks for your answer. Yes, it's a 2D game and I'd appreciate the link you were talking about.

However, about your answer, don't you end with a gigantic Input $$anonymous$$anager script full of if/elseif ?

avatar image
0

Answer by torrente · May 21, 2014 at 02:34 PM

I've had success doing it this way:

Tag each object that you want as clickable with a "clickableObject" tag in the inspector.

Code:

 Ray clickRay;
 float distance; //need to put in some value depending on what you need
 
 ...
 ...
 
 
 //the following would usually go in Update()
 
 clickRay = mainCamera.ScreenPointToRay(Input.mousePosition);

 if (Input.GetAxis("Fire")
 {
  if (Physics.Raycast(clickRay.origin, out objHit, clickRay.direction * distance))
    {
      if (objHit.collider.tag == "clickableObject")
       {
         //do something that you can only do on a clickable object
       }
    }
  }
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 lvictorino · May 21, 2014 at 03:39 PM 0
Share

Cool, but how do you manage object that already have a tag? How do you manage different clickable object? With a test after your collider.tag test?

avatar image torrente · May 21, 2014 at 04:31 PM 0
Share

If an object already has a tag, I may look at creating a subcategory. This would go back to the planning phases of game design. You can create a ton of different tags (I've never run out) and they do not seem to impact the game. As for once it is deter$$anonymous$$ed that an object is clickable, I do use additional tests to find more specifics of the object clicked on. You could go so far as to check for a unique object ID using getInstanceID() which will return an int. I built a horror game demo where you had to go around and collect specific objects (like Slenderman). When the player clicked on the object, I displayed a message stating "you have found the", name.

That's my usual approach. I'd also suggest to try and get all of the game mechanics down before actually working on the game. "Feature creep" can break any project, sending you back to the drawing board over and over again...

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

25 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

InputManager doesn't detect gamepad numbers correctly 1 Answer

How do I separate OnMouseDown() from OnMouseDrag() 0 Answers

Is there a way to modify Input Manager so that an ax is be able simulate an input from code? 0 Answers

how to make a configurable input 1 Answer

Input Manager Snap for Unity 4.3 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