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 miszelfox · Nov 07, 2014 at 09:52 PM · guitextureobject

[SOLVED] How to check when GUI texture is on 3D object?

I want to check when 3D object (Enemy) is targeted by 2D GUI texture (like crosshair) to for example make it red when i am pointing on an enemy.

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 · Nov 07, 2014 at 10:10 PM 0
Share

First are by 'GUI texture' do you mean a GUITexture object? If true on the first part, how are you setting up your pixelInset values and are you manipulating them at runtime or are you using Transform.position?

avatar image miszelfox · Nov 07, 2014 at 10:25 PM 0
Share

To be honest i didn't make it yet, but i would use GUI Texture object and make it always on center.

2 Replies

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

Answer by robertbu · Nov 07, 2014 at 10:55 PM

A GUITexture has a pixelInset Rect. Make sure that the x and y values are setup as negative 1/2 of the width and height. For example if you use 256 for the width and height of your crosshair, the pixelInset values would be (-128, -128, 256, 256).

With these pixel inset values, the 'pivot' will be the center of the texture. GUITextures live in Viewport space, so to Raycast() you would would do:

  var pos = Camera.main.ViewportToScreenPoint(transform.position);
  var ray = Camera.main.ScreenPointToRay(pos);
  var hit : RaycastHit;
  if (Physics.Raycast(ray, hit) {
       Debug.Log("I hit "+hit.collider.name+", "+hit.collider.tag);
       if (hit.collider.tag == "SomeInterestingTag") {
             // Do something here
       }
  }

Note that if you have your crosshair following the mouse, you can use Input.mousePosition and not convert from Viewport to Screen coordinates.

Comment
Add comment · Show 3 · 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 miszelfox · Nov 09, 2014 at 12:39 PM 0
Share

I am using area crosshair, a rectangle for example, what now? $$anonymous$$y crosshair is part of one GUI image which is stretched to fill screen, i would like to check if my enemy is inside of this rectangle (let's say that i know pixel positions of this rectangle)

avatar image robertbu · Nov 09, 2014 at 03:35 PM 0
Share

The solution can either be fairly simple or very complex depending on how you define things. For example, since you stretch your GUI texture to fill the screen, the hit box will have the same viewport coordinates no matter what device. Viewport coordinates start at (0,0) in the lower left of the screen and go to (1,1) in the upper right. So say your hit box is is the middle quarter of the screen. You can create a Rect:

   var hitRect = Rect(0.375, 0.375, 0.25, 0.25);

Then if you want to detect if the pivot point of a given object is inside the rect on the screen you can do:

  var pos = Camera.main.WorldToViewportPoint(someObject.position);
  if (hitRect.Contains(pos)) {
       // Do 'someObject' is inside
  }

Note this check is just the pivot point. If you want to know if any part of the object is inside, then the calculation is uncertain. You can check to see if any corner of the bounds of the object are inside the rectangle, but for irregular shaped objects, this will give you some false positives.

avatar image miszelfox · Nov 09, 2014 at 05:57 PM 0
Share




EDIT: Okay never$$anonymous$$d, i had to change "hitRect" name to something different and now it works, thank you very much





*Okay it almost works. "Almost" because it's always "true" even if my object is not between rect borders (but it still has to be visible on screen, if it's not it's "false") var hitRect = Rect(0.45,0.45,0.55,0.55); var enemy : GameObject;

 function Update () {
 var pos = kamera.WorldToViewportPoint(enemy.transform.position);    
 if (Input.GetButtonDown("action"))
 $$anonymous$$yFunction(pos);
 }
 
 function $$anonymous$$yFunction (pos : Vector3) {
     if (hitRect.Contains(pos)) {
        print("HIT");
       }
 
 }*

avatar image
0

Answer by SkaredCreations · Nov 07, 2014 at 10:16 PM

You have to cast a ray from the camera and if it hits an enemy then change the "color" property of your guiTexture, check the documentation of Physics.Raycast

Comment
Add comment · Show 1 · 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 miszelfox · Nov 07, 2014 at 10:31 PM 0
Share

Why from camera? I want it to be accurate, what if i am using 3rd person camera?

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

27 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotating GUI Texture By Angle 2 Answers

How to GUI texture image change? 1 Answer

Gui Texture GameObject not showing up 1 Answer

Can you change time scale to zero but still have GUI textures changing? 2 Answers

GUI Style Issue-- Texture not changing 2 Answers


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