- Home /
Raycasting forward from screen perspective instead of game position.
Hi, I'm making a game where you can pick up and move objects when an object is placed in front of them. This object has a raycast attached which enables the movement script when it finds a moveable object. However, the Ray travels forwards in terms of game position, which means when you play the game, you actually have to be slightly left or right of the object for the ray to hit, which isnt very intuitive.
So basically, I want to shoot a ray forward from the object whilst moving forward in terms of the screen position. It needs to start from the object, not the camera.
I think I can do this with Camera.WorldToScreenPoint, but, being completely honest, after trying several different ways, I figured I honestly have no idea how to use that code. Can anyone help me figure it out?
Here's the Raycasting portions of my script, although it's pretty standard:
function Update () {
var fwd = transform.TransformDirection (Vector3.forward); //Defining the travel direction of the raycast vector var hit : RaycastHit; //If Raycast collides with something, that becomes the 'hit' //var illuminate : RaycastHit; var LayerMask = 1 << 9; //Interacts with 1 layer - Layer 9 - Moveable
//Draws ray vector in Scene view Debug.DrawRay(transform.position, fwd * 50, Color.green);
(Physics.Raycast (transform.position, fwd, hit, 50, LayerMask));
I read this three times and I have no idea what you're asking. I think the current camera transform.forward is what you want. But I can't be sure.
Camera.current will usually be null when not using it at the right place (For editor stuff and image processing), Camera.main is generally what you want
Answer by Mike 3 · Mar 11, 2011 at 08:28 PM
var fwd = Camera.main.transform.forward;
//snip
Physics.Raycast (transform.position, fwd, hit, 50, LayerMask);
You can store Camera.main in start if you prefer, so you don't need to look it up every time
Edit:
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
ray.origin = transform.position;
if (Physics.Raycast(ray, hit, 50, LayerMask))
{
//code
}
I tried this, but it isn't what I meant. It draws the Ray from the camera ins$$anonymous$$d of the object the scripts attached too. It seems I've not been clear so I'll try and explain again. I need the ray to be drawn from the object the script is attached to. However the ray needs to move straight forward in depth in terms of screen position rather than forward in the world space. $$anonymous$$y code generates the ray moving forward in world space which isn't directly forward in screen space, making it hard to deter$$anonymous$$e what its hitting or not.
Sorry if I'm unclear or whatever.
just change Camera.main.transform.position to transform.position, rest stays the same
Thanks, but it still doesnt seem to work. To pick up the object i need to go to the left or right of it depending which side of the screen its on. I want it to function more like if it was being clicked on by a mouse - so it only activates if the object that picks it up is directly in front of it from the cameras viewpoint. Your code seems like it's exactly what I need, so I'm a little confused why it's not working.
I appreciate the help though, thanks
$$anonymous$$aybe something like the edit, though I'm kind of guessing using my interpretation of what your game is like ;)
Your answer
Follow this Question
Related Questions
How to rotate a camera like Google Earth? 0 Answers
camera detecting walls 1 Answer
Correcting Object Offset on WorldToScreenPoint 0 Answers
Raycast hitting objects to the left of my player 1 Answer
Editor Camera Perspective 1 Answer