- Home /
Click On/Off Manager - C#
Hey,
So I'm basically making an enemyAI script and what i want to do is make that particular enemies HUD box display if i click on it OR if i click on another enemy then the previous HUD box closes and the new one opens.
I figure you will need to have a raycast, I'm really bad at raycasting, i attempted google/youtube searches but I couldn't find anything that i could use. Can someone assist me please(In c#)?
Many thanks
Answer by oliver-jones · Sep 15, 2013 at 06:26 PM
Hey bud,
Yes, you are correct, you will indeed need to use raycasting - which is eeaassy! Honestly!
We will be using this: (in JavaScript, sorry :/)
function Update() {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 500)) {
Debug.DrawLine (ray.origin, hit.point, Color.green, 1);
}
}
--- C# ---
void Update() {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
Debug.DrawLine(ray.origin, hit.point);
}
So what this is doing is firing an invisible line (well, actually it will fire a green line within the editor), from the camera, to whatever the mouse is hovering over within the scene.
Next we want to check if the user has clicked on anything within the scene. So inside that if statement ^ we want to add this:
if(Input.GetButtonDown("Fire1"){
print("We have clicked on : " + hit.collider.transform.name;
}
Now, we need to check if we have clicked on an enemy. First of all, make sure all your enemies have a tag "Enemy" applied to them, then we can do this, within that if statement ^:
if(hit.collider.gameObject.tag == "Enemy"){
print("We have clicked on an enemy");
}
We then want to send a message to that specific enemy we just clicked on, this message will basically say "Mate, we've just clicked on you, so do something special", so within that if statement ^ add:
hit.collider.SendMessageUpwards("EnemyClick", SendMessageOptions.DontRequireReceiver);
Now, all we need to do now, is to have a receiver for this message, on the enemy. So, make a new script... or add this to a script already attached to your enemy, and make a function called:
void EnemyClick(){
//Turn on HUD objects
}
This function will get called every time the enemy is clicked on.
Thats it! Only thing I haven't added is that when you click on one enemy, the other enemies HUDs will NOT turn off... But I'm sure you can figure that out, I'll give you a hint though - you will need to make 2 variables, one called currentEnemy, and another called prevEnemy, you will then want to send a 'EnemyClickOff' message to the prevEnemy :)
void EnemyClickOff(){
//Turn off HUD objects
}
Great Answer!
Especially like how you included the debug stuff which I never tried previously but am doing so now:
Debug.DrawLine (ray.origin, hit.point, Color.green, 1);
Very Helpful!
Glad I can help - remember to tick the answer on this forum :)
Answer by Jamora · Sep 16, 2013 at 01:48 AM
You might not have to do any raycasting. MonoBehaviours have a callback OnMouseDown(), which gets called if there is a collider attached to the gameobject and LMB was pressed over that object; essentially the same as raycasting.
If you have different things happen at different times ( a state machine) then using OnMouseDown might be the worse idea, though it all depends on how clever you are with your design.
All you need to do is add
void OnMouseDown(){
//Do whatever needs to happen on a mouse press.
//Possibly the SendMessage from oliver's answer.
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613114228im_/https://answers.unity.com/themes/thub/images/avi.jpg)