Scripting Approach to Mouse interaction
Hello,
I'm a complete newbie to Unity 3d and would appreciate feedback to my ideas. I've looked at the unity tutorials, documentation, and many resources to find how to deal with mouse based object selection. While i understand HOW to select an object, im unsure what a good approach is to manage my scripts and data once i select the object i want. Let me back up a bit...
I'm prototyping a 3d isometric gamesimilar to pillars, BG II etc. (i know they arent 3d, but just to get a reference). I will be using the mouse (or touch) to interact with the world and with enemies. For example, im a wizard character and click on an enemy to see their extended stats in a tooltip, Then i click behind them to move, and finally click on a spell and click the enemy to cast it. How should i approach this problem of making sure i know what im clicking on and when it's appropriate to do what action.
My ideas are:
A singular delegate event system that tracks clicks that objects can subscribe to. I would raycast to the mouse (with a Raycastall[] or something) and figure out what's being clicked. Im left with the problem of dealing with scattered code and passing information of the previously clicked object around.
I'd implement an interface solution iPointerClickHandler on Every clickable object i care about in the game. I would then pass the gameObject to a static singleton click manager that keeps track of what's been clicked (in a list or something) and using a bunch of if statements call the necessary functions that way. 3.
I'm not sure how to do this in C# as i come from a javascript node background, but something along the lines of async callbacks with CustomGameObject.onClick( args, 'function to perform', callback); I would use tags to figure out the type of object clicked and use my clicker function to perform necessary actions then continue with the game state via callbacks
Please let me know if one of these approaches is straightforward or if there's another, easier way to accomplish what im trying to do. Thank you very much!
I'm going with option 2 because it makes the most sense to me for the time being. I'll check back later and reevaluate my approach if someone has feedback
Your answer
![](https://koobas.hobune.stream/wayback/20220612034938im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make my flashlight move with the mouse movement? 0 Answers
I need help with learning... 1 Answer
Press the button play music in the next scene. 1 Answer
First person shooter : Leaning! 2 Answers