- Home /
Raycasting forward in screen perspective
Ok, I tried explaining this before, but it seemed hard to explain and not many people understood what I was trying to do. I'm hoping this image will help, but honestly, I'm not optimistic.

In the above image you can see my problem. In my screen shot on the right hand side are two cubes - the front most one is controllable by the player and is raycasting forward. If the ray hits an object it can be picked up and moved around. However, in order for the ray to hit the crate behind it (the one slightly to its left), it needs to be held in the position shown because of the way the camera perspective works.
Obviously this isn't very user freiendly (they have to try and work out how far to either side to move the object to pick it up) so I want the ray being cast from the object to move forward from the main cameras perspective rather than in worldspace - thus allowing you to pick up an object that appears directly behind you when you play the game.
Please tell me if you need anything clarified and I'll do my best, but even trying to explain this much has given me trouble.
Anyway, here is my code so far (in java, attached to the controllable raycasting cube:
var hasObject: boolean = false; var otherThing: Transform;
function Update () {
var fwd = Camera.main.transform.forward; var hit : RaycastHit; //only finds objects in the 'moveable' layer var LayerMask = 1 << 9;
if (Input.GetButtonDown ("Fire2")) { if (!hasObject) { //if (otherThing){ if (Physics.Raycast(transform.position, fwd, hit, 50, LayerMask)) //if (Physics.Raycast(ray, hit, 50, LayerMask)) {
otherThing = hit.transform; otherThing.rigidbody.isKinematic = true; otherThing.parent = transform; otherThing.Find("Glowblock").light.enabled = true; hasObject = true; print("Picked up "+otherThing.gameObject.name+"\n"); }else { print("Nothing to pickup!\n"); } } else { otherThing.rigidbody.isKinematic = false; transform.DetachChildren(); otherThing.Find("Glowblock").light.enabled = false; hasObject = false; print("Dropped "+otherThing.gameObject.name+"\n")
} } }
can you clarify? post more screenshots. the second diagram doesn't look correct to me. Perhaps you can explain it's shape...
I'll post more screenshots later. The second diagram is meant to show how the camera perspective looks when you play the game, as it narrows to a point in the distance.
Answer by Owen-Reynolds · Mar 14, 2011 at 02:49 AM
So, to clarify, you can pick something up when box#1 looks like it is in front of box2.
If this is the case, you can think of box#1 as a cursor being moved over an item to select. It's just more difficult to move than a real cursor.
The standard solution is to shoot a ray from the camera through the box#1. For a cursor, you have to convert it from screen pos to world pos using ScreenPointToRay and raycast from the camera through it. In your case, everything is in world pos, so you can skip that step. Try:
Raycast(transform.position, transform.position-camera.position, ... )
^^ from ^^ direction
Yes, I want to pick it up when it looks directly in front of it, just like a cursor. At the moment, the camera perspective distorts that and means I need to move the object slightly left or right of the object i want to pick up. I think that's what you're saying here, so I'll try your method and report back later.
Shouldn't the first box shouldn't be picked up by the raycast anyway because it's not in the moveable layer. I'll make sure I do that anyway.
Gives me an error: Assets/Scripts/$$anonymous$$ovement script.js(57,46): BCE0019: 'position' is not a member of 'UnityEngine.Camera'.
never $$anonymous$$d, i was being stupid. I just need camera.transform.position.
Works great, thank you!
About the first box, I just realized you could go from it. Editting my response.
Your answer