x


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));
more ▼

asked Mar 11 '11 at 08:07 PM

Alienjesus gravatar image

Alienjesus
103 32 34 40

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.

Mar 11 '11 at 08:14 PM flaviusxvii

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

Mar 11 '11 at 09:05 PM Mike 3
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first
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
}
more ▼

answered Mar 11 '11 at 08:28 PM

Mike 3 gravatar image

Mike 3
30.5k 10 65 253

I tried this, but it isn't what I meant. It draws the Ray from the camera instead 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. My code generates the ray moving forward in world space which isn't directly forward in screen space, making it hard to determine what its hitting or not.

Sorry if I'm unclear or whatever.

Mar 11 '11 at 09:27 PM Alienjesus

just change Camera.main.transform.position to transform.position, rest stays the same

Mar 11 '11 at 09:30 PM Mike 3

edited the answer to make it a little more obvious :)

Mar 11 '11 at 09:31 PM Mike 3

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

Mar 11 '11 at 09:39 PM Alienjesus

Maybe something like the edit, though I'm kind of guessing using my interpretation of what your game is like ;)

Mar 11 '11 at 09:51 PM Mike 3
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3014
x1535
x57
x25

asked: Mar 11 '11 at 08:07 PM

Seen: 1426 times

Last Updated: Mar 11 '11 at 08:07 PM