- Home /
[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.
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?
To be honest i didn't make it yet, but i would use GUI Texture object and make it always on center.
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.
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)
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.
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");
}
}*
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
Why from camera? I want it to be accurate, what if i am using 3rd person camera?
Your answer
Follow this Question
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