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 greatwhiteshark17283 · Aug 15, 2013 at 10:54 PM · guishootingaimdetectover

How to detect if a GUI texture is over an object

I am making a shooting game and I had a GUI texture to show what the player is aiming at (If this doesn't make sense, here is a picture:alt text). I want it so then if the player is aiming at an enemy, the game would detect it and let the player take health out of the enemy. How could I do this (please in JavaScript)?

unity answers screenshot.png (416.3 kB)
Comment
Add comment · Show 2
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 robertbu · Aug 16, 2013 at 02:40 PM 0
Share

Are you talking about a GUITexture or are you drawing using GUI.DrawTexture()? Is the position the mouse position, or something else? If it is a GUITexture, do you have the PixelInset set so that the cross is at the center of the position? Are you trying to blind fire a particular direction, or do you have object that you want to target?

avatar image greatwhiteshark17283 · Aug 16, 2013 at 02:49 PM 0
Share

I'm talking about a GUITexture. The Pixel Inset is X=0, Y=0, Width=20, Height=20. That makes it about centered. When the player shoots, there isn't actually a bullet being cloned in the scene. What I want is if the GUITexture is over an enemy, it will take health out.

1 Reply

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

Answer by robertbu · Aug 16, 2013 at 03:01 PM

Assuming your cross is centered in the graphic, you will want to set your Pixel Inset to 10 and 10. This will anchor the center of the cross at the GUITexture position. To detect enemies you will use raycasting. GUITextures live in Viewport space, so you can create a ray from the position. I don't have you code nor know your game mechanic, so you will need to adjust this script for your game. It fires on a left mouse click. If it finds an enemy under the cursor (a game object tagged 'Enemy', it get the enemy health component from the enemy object and calls 'ApplyDamage()'.

 #pragma strict
 
 function Update() {
     if (Input.GetMouseButtonDown(0)) {
         var ray = Camera.main.ViewportPointToRay(transform.position);
         var hit : RaycastHit;
         if (Physics.Raycast(ray, hit)){
             if (hit.collider.tag == "Enemy") {
                 var enemyScript = hit.collider.gameObject.GetComponent(EnemyHealth);
                 enemyScript.ApplyDamage();
             }
         }
     }
 }
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

15 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

Related Questions

Detect GUI rotation degree 1 Answer

transform.TransformDirection not working -- WHAT IM MISSISNG? 1 Answer

how do i make my own GUI 1 Answer

2D Aiming and Shooting at the mouse 3 Answers

Detecting a GUI element under the mouse? 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