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 Iceblitzyt · Sep 15, 2013 at 06:04 PM · c#raycast

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

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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
 }
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 RyanZimmerman87 · Sep 15, 2013 at 11:44 PM 0
Share

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!

avatar image oliver-jones · Sep 16, 2013 at 10:16 AM 0
Share

Glad I can help - remember to tick the answer on this forum :)

avatar image
0

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.
 }

Comment
Add comment · 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

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

18 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

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Raycast - arrow stopped by walls (layer) but not by units (layer) 1 Answer

2 raycasts on the fpwalker/camera 1 Answer

how do i make my objects not spawn on already spawned objects 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