I want to be able to raycast from a given screen x an ever changing y position but I cant figure out how to do it or if its even possible.
Any help is greatly appreciated!
Just swap out the mouse position for your own screen position. This is with mouse:
//Camera.main.ScreenPointToRay(Input.mousePosition)
Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity);
So perhaps use the same code, but instead of Input.mousePosition, use your own Vector2:
Vector2 screenPos = new Vector2( 300, 400); // your screen position
Physics.Raycast(Camera.main.ScreenPointToRay(screenPos), out hit, Mathf.Infinity);
This assumes that Input.mousePosition is a Vector2 (which I am only assuming).
Worth a try?
I couldnt seem to get that to work pixelthis.
I tried this
var target1: Transform;
var target2: Transform;
function Update() {
var ray: Ray = Camera.main.ScreenPointToRay(Vector2(512,768));
var hit: RaycastHit;
if (Physics.Raycast(ray, hit)) {
if (hit.transform == target1) {
print("Hit target 1");
} else if (hit.transform == target2) {
print("Hit target 2");
}
} else {
print("Hit nothing");
}
}
and I am useing a game resolution of 1024x 768 and in theory Vector3(512,768,0)); should be half way in my screen and all the way at the bottom, but its actually no where near that, its close to the the top and not even half way on my screen? Any ideas?
Edit:
Ok I looked into the this a little further
C:\Program Files (x86)\Unity\Editor\Data\Documentation\Documentation\ScriptReference\Camera.ScreenPointToRay.html
Found that bottom left is 0,0 so that makes sence now but when I run this:
print (“Camera is " + camera.main.pixelWidth + " pixels wide”);
it changes depending on my aspect ratio and everything so I use this
var ray: Ray = Camera.main.ScreenPointToRay(Vector2(camera.main.pixelWidth/2,100));
to get half way in between my screen but the problem is the y value in the vector 2 is going to be always changing.
I thought I post my hole code and give a better explanation of what I am trying to do:
var Crosshairtex : Texture;
private var limitmovedownY = 0.0;
private var mouseinputY = 0.0;
var movedownY = 0.0;
var sensitivityY = 10;
var target1: Transform;
var target2: Transform;
function Update() {
//print ("Camera is " + camera.main.pixelWidth + " pixels wide");
//print ("Camera is " + camera.main.pixelHeight + " pixels high");
//print (movedownY);
var ray: Ray = Camera.main.ScreenPointToRay(Vector2(camera.main.pixelWidth/2,movedownY));
var hit: RaycastHit;
if (Physics.Raycast(ray, hit)) {
if (hit.transform == target1) {
print("Hit target 1");
} else if (hit.transform == target2) {
print("Hit target 2");
}
} else {
//print("Hit nothing");
}
mouseinputY -= Input.GetAxis("Mouse Y");
mouseinputY = mouseinputY - limitmovedownY;
limitmovedownY = mouseinputY;
movedownY = movedownY + mouseinputY * sensitivityY;
if (movedownY < 0){
movedownY = 0;
}
if (movedownY > 350){
movedownY= 350;
}
}
function OnGUI () {
GUI.matrix = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, Vector3
(Screen.width / 1024.0, Screen.height / 768.0, 1));
GUI.DrawTexture (Rect (500,movedownY,24,24), Crosshairtex);
}
Basically I am trying to match up the y value in the raycast with where the GUI.DrawTexture (Rect (500,movedownY,24,24), Crosshairtex);
is on the screen, but im not sure how to do this.
Any help is greatly appreciated!
The Camera.ScreenPointToRay function is usually used with the mouse position, but you can pass any Vector3. The X and Y components determine the screen position and the Z component is ignored. You just need to pass the top and left coordinates you are using with the rectangle for GUI.DrawTexture (perhaps with an offset to get the position in the middle of the crosshair):-
var leftOffset: int;
var topOffset: int;
...
var point = new Vector3(500 + leftOffset, mouseDownY + topOffset, 0);
var ray = Camera.main.ScreenPointToRay(point);
if (Physics.Raycast(ray)) {
...
}
I still have the same problem for example if I use
var point = new Vector3(512 , 768, 0);
var ray = Camera.main.ScreenPointToRay(point);
if (Physics.Raycast(ray)) {
print("Hit");
}
My standard resolution that I use is 1024768 and thus 512 is half my screen and 768 is right at the top, if I build the game and run the game in 1024768 it works perfectly, if I say run it in 640*480 it no longer works correctly. It also doesnt work correctly in the editor either because it takes into account the hole window the unity editor uses. So what I trying to do is make it work accross all resolutions like I do with my gui with this code:
GUI.matrix = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, Vector3
(Screen.width / 1024.0, Screen.height / 768.0, 1));
Any suggestions?
You need to multiply the screen point by the GUI matrix to get the transformed screen position:-
var point = new Vector3(512 , 768, 0);
var transPoint = GUI.matrix.MultiplyPoint3x4(point);
var ray = Camera.main.ScreenPointToRay(transPoint);
...etc...
Well it looks like it works in windows exe builds, it doesnt work in the editor however? I am not sure why, does anyone know? Will it work with other platform builds like webplayer and macos?
Using a hard-coded location is probably a bad idea, even if you’re transforming it by the matrix. Personally, I’d use Screen.width and Screen.height to calculate the right position:
new Vector2(Screen.width, Screen.height / 2)
The problem starts when I want to have resolution independent gui and I am moving the gui aimer up and down, its not a set position. I have it working, just doesnt work with the editor and im not sure of mac os or website builds, windows exe build work correctly in any resolution.
This code creates a ray from the same rect you use to draw the gui aimer. If the gui is in the right place, this should make the correct ray.
Ray GuiRectToRay( Rect r )
{
Vector3 p = new Vector3(
r.x + r.width / 2,
Screen.height - (r.y + r.height / 2)
);
return Camera.main.ScreenPointToRay( p );
}
I tried that code, didnt work, but then I started thinking, I figured you basically meant instead of defining the x and y based on 1024 x 768 just use screen.width / 2 and screen.height - movedownY.
Ill give it a try
Interestingly enough I tried that it didnt work, then I went back to my original code of using 512,768 and it started working not only in the builds but in the editor as well.
Final code if interested:
var point = new Vector3(512, 762 - movedownY, 0);
var transPoint = GUI.matrix.MultiplyPoint3x4(point);
var ray = Camera.main.ScreenPointToRay(transPoint);
var hit: RaycastHit;
Thats based on this:
GUI.matrix = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, Vector3
(Screen.width / 1024.0, Screen.height / 768.0, 1));
GUI.DrawTexture (Rect (506,movedownY,12,12), Crosshairtex);
Thanks for all the help everyone, topic solved!