• 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 Piotrone · Sep 29, 2016 at 08:20 PM · tagsevents

Calling methods depending on number of mouse clicks and tags on an object

Hi all,
How can I improve my code to call different methods depending on how many times an object was clicked and on how it was tagged?

I have 3 objects (player, player1 and a child object), with 3 different tags (catcher, thrower and energy), 2 types of clicks (single and double) and a series of actions (move, attack, select...)

Example of combinations are:

single click on thrower > select thrower
single click on energy (child of thrower) > select thrower
single click on catcher > select catcher
single click on world and no collider found > thrower or catcher can move
double click on thrower > cast energy, select thrower
double click on energy (child of thrower) > cast energy, select thrower
double click on catcher > attack, select catcher

I now have two type of scripts:

1/an input manager that counts the clicks and fire two events (single and double click)

2/the actions scripts (playerMove, playerAttack,selectPlayer,...) that are subscribed to the single and double click events, that cast a ray to check if the object clicked has the right tag to perform the action

An issue I have is all the duplicate ray casting code, in all action scripts. I thought of moving them into the input manager but then I'll end up with too many events, one for every click type and tag.

Is there a more elegant way to solve this? Thanks

 using UnityEngine;
 using System.Collections;
 
 public class UserInputManager : MonoBehaviour
 {
     private bool mouseClicksStarted = false; 
     private int mouseClicks = 0; 
     public float mouseTimerLimit = .25f;
     public delegate void SingleClickEventHandler (RaycastHit2D hit, Vector2 destination); 
     public static event SingleClickEventHandler singleClick; 
     public delegate void DoubleClickEventHandler (RaycastHit2D hit); 
     public static event DoubleClickEventHandler doubleClick;
     private RaycastHit2D hit;
     private Vector3 destination;
 
     void Update() 
     {
         if (Input.GetMouseButtonDown (0)) {
             destination = Camera.main.ScreenToWorldPoint (Input.mousePosition);
             hit = Physics2D.Raycast (destination, Vector2.zero);
             mouseClicks++;
             if (mouseClicksStarted) {
                 return;
             }
             mouseClicksStarted = true;
             Invoke ("CheckForDoubleClick", mouseTimerLimit);
         }
     }
     private void CheckForDoubleClick() 
     {
         if(mouseClicks > 1)
         {
             if (doubleClick != null) 
             {
                 doubleClick (hit);
             }
         } else 
         {
             if (singleClick != null) 
             {
                 singleClick (hit, destination);
             }
         }
         mouseClicksStarted = false;
         mouseClicks = 0;
     }
 }

Example of action, in this case player movement

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
     public float travelSpeed = 5f;
     public bool PlayerCanMove;
     private PlayerSelection playerSelection;
     private Vector2 finalDestination;
 
     void OnEnable()
     {
         UserInputManager.singleClick += CheckForPlayerDestination;
     }
 
     void OnDisable()
     {
         UserInputManager.singleClick -= CheckForPlayerDestination;
     }
         
     void Start () 
     {
         playerSelection = GetComponent<PlayerSelection>();
     }
 
     void FixedUpdate () 
     {
         if (playerSelection.active && PlayerCanMove) 
         {
             Move ();
         }
     }
 
     public void CheckForPlayerDestination(RaycastHit2D hit, Vector2 destination) 
     {    
         if (hit.collider == null) 
         {
             finalDestination = destination;
             PlayerCanMove = true;
         } else 
         {
             PlayerCanMove = false;
         }
     }
 
     public void Move() 
     {
     
     transform.position = Vector2.Lerp (transform.position, finalDestination, 5 * Time.deltaTime);
     }
 }
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Where can I find complete list of events? 1 Answer

Best use cases for an observer pattern messenger system 0 Answers

Raise and Event with EventMessenger 0 Answers

What happens with the receiver when the same event is trigger from multiple instances at the same time? 0 Answers

Event is always null, despite adding a function to it. 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