- Home /
Get GUITexture to follow ScreenPointToRay ray cast
I have a GUItexture that I would like to follow a raycast.
Current Setup:
Vector3 Finger = GetWorldPos(fingerPos);
float Finger_x = Finger.x / Screen.width;
float Finger_y = Finger.y / Screen.height;
float Finger_z = Finger.z;
GameObject.Find("TestingFinger").transform.position = new Vector3(Finger_x,Finger_y,0);
/* GetWorldPos function is as follows:
public static Vector3 GetWorldPos( Vector2 screenPos )
{
Ray ray = Camera.main.ScreenPointToRay( screenPos );
// we solve for intersection with z = 0 plane
float t = -ray.origin.z / ray.direction.z;
return ray.GetPoint( t );
}
*/
However I am getting odd results. (0,0) is the bottom left of the screen and (1,1) is the top right of the screen.
When my mouse/finger is at (0,0) the Guitexture is at (1,1) I think the issue could be that the raycast is returning (0,0) as the middle of the screen. Any ideas how to convert this so the GUItexture will follow the mouse. I have a video of how it is acting "odd" here: http://db.tt/c2H55tO8
Thanks.
The difference i coordinate space between screen and gui has caused problems for me before too. but usually the process was only ever for debugging or temp stuff so never really looked for a clean workaround. One quick and probably dirty sollution is just to invert the coords your using at one end of the process.
so just 1-value it at see where that gets you.
Answer by Owen-Reynolds · Jan 26, 2013 at 06:11 PM
Won't the final position of the GUITex be just under your finger? Going from finger to world, then back to screen -- won't that just take you back where your finger was? If not, where? Could just do that with: Finger_X=Input.mousePosition.x/Screen.width;
and get rid of the ray.
Say you wanted the GUIText to be centered over the object your finger is touching, then you'd need it all -- ray cast, take pos = HIT.transform.position, and back to the screen with Camera.main.worldToViewPort(pos)
.
But, in what you have, GetWorldPos
is returning ray.GetPoint()
, which is a spot in the game world, in meters. Then it gets used as pixels. In general that will give random results (based on camera pos.)
Your answer
![](https://koobas.hobune.stream/wayback/20220613091451im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
GUI texture touch input problem 1 Answer
How can I add a distance cap to this script? 3 Answers
GUI.DrawTexture on GUI.Button press 1 Answer
GUI_TEXTURE PROBLEM! 2 Answers