|
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:
(comments are locked)
|
You can store Camera.main in start if you prefer, so you don't need to look it up every time Edit: 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)
|

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