- Home /
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
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.
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:
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.
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
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.
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
}
}