- Home /
Raycast using an object in world space? (Converting to Screen Space?)
UPDATED - Thanks to a commenter pointing out a boneheaded mistake, I am re-wording this problem...
I've been pulling my hair out on this one - I am making a two-player game. Player one moves an object around a flat plane with the mouse. Player two moves around with the arrow keys.
I'm using Input.mousePosition to build a raycast for player one and it works great. I'm having no luck building a functioning raycast for player two, however. I know the difference between world and screen space, but when I try to convert the position of the player 2 object, my ray is pointing from the camera to some point in space.
Here's my (revised) code. I've tried several things that involve manipulating the math, but I'm about at my wit's end with it. Any input would be great. I think I just have yet to fully wrap my head around handling screen/world coords. NOTE - cursor_p2 is an object in the world which the player controls. Despite the name, it isn't a GUI object.
After correcting the badly-written Debug.DrawLine(), I now see that I'm much closer than originally anticipated. But as shown in the image, my raycast isn't accurate. It shoudl be pointing at the cursor object, but is hitting about 10 units off from it.
// Try converting cursor world position to screen/pixel coords...
p2Point = Camera.main.WorldToScreenPoint(cursor_p2.transform.position);
var p2ray : Ray = Camera.main.ScreenPointToRay(p2Point);
if (Physics.Raycast(p2ray.origin, p2ray.direction, p2hit)){
// build some raycast info to feed children objects
p2hitObj = p2hit.collider.gameObject;
//Debug.Log("P2 ray hit " + p2hitObj.name);
p2point = p2hit.transform.position;
} else {
Debug.Log("p2 raycast failed");
}
Debug.DrawLine(Camera.main.transform.position,p2hitObj.transform.position,Color.yellow);
![alt text][1] [1]: http://answers.unity3d.com/storage/temp/1242-Untitled.png
Note your Debug.DrawLine() draws a line from the camera position (in world space) to the cursor position (in screen space), which doesn't make sense.
Also, we would need more information about cursor_p2 - is this a world space game object that simulates a cursor? How do the keys modify its position? Is the goal to move the corsur_p2 parallel to the screen using the keys?
cursor_p2 is a prefab in the world - basically a textured plane which is simply translated 10 units (the size of the game plane's grid) in a direction when the corresponding arrow key is hit.
I don't need the raycast for movement (though I may change that) - but I need to figure out what the "cursor" object is over, so I planned on using this to do a filtered raycast for my terrain units.
I still don't get it. So your cursor_p2 is a plane parallel to your game grid (or is it a Unity terrain which also can vary in height?) and you want to cast a ray from the camera through cursor_p2 to figure out which object on your grid is being hit?
Sounds like somebody needs a screenshot. Screenshot it. And edit the details of your question onto it (helps more than a bad explanation).
Answer by Wolfram · Jun 13, 2012 at 12:29 PM
Hm, should have answered this immediately, but the solution was so simple, so I thought you were trying to do something different :-D
Just forget about the screenspace stuff. The ray you're looking for is simply the line from your camera position to the cursor_p2 position, both in world spacce:
if (Physics.Raycast(Camera.main.transform.position,
cursor_p2.transform.position - Camera.main.transform.position))
{
p2point=cursor_p2.transform.position;
}
Make sure the prefab's pivot(="origin") really is the center of your plane, not some point between your prefab's light and the plane, or something else.
EDIT: if you're interested in the actual intersection with your grid, you can also include the p2hit info. In this case, p2hit.point will contain the true intersection point, and p2hit.transform.position will contain the center(=pivot) of the object that was hit. All this assuming that cursor_p2 has no collider, or is not in a layer that can be hit by the Raycast of course.
Answer by Owen-Reynolds · Jun 13, 2012 at 06:47 AM
Your DrawLine
is still off from where the ray is going. Use p2hit.point
for the endpoint.
The problem is, suppose the ray hits cow C in the head. You draw the line to C.position
, which is the cow's origin. If you ever hit the terrain, the Line goes completely off, to the origin of the terrain -- (0,0,0).
Your answer
Follow this Question
Related Questions
How to get 3D worldspace position from 2D touch/mouse position 3 Answers
Diagonal Raycasting to detect platform returns true, even when false 1 Answer
Raycast hits and then doesn't fire again? 2D Game Raycasting Problem 1 Answer
My Raycasts seem to sometimes miss 0 Answers
Raycast not hitting the collider properly it always has a weird offset 0 Answers