- Home /
Problems with raycast
I'm using a script that another person helped me with to detect if the player is looking at an object that he can pick up and then picking it up. I'm using raycasts for this and it's always been inaccurate as hell and i couldn't find out why. So i debugged it with:
if (Input.GetKeyDown(KeyCode.F))
{
Debug.DrawLine (ray.origin, hit.point, Color.red,2);
}
and found that the line goes to 0,0,0 in the world instead of forward from where the camera is facing, picture shows the lines
You can see I'm looking up but the lines still go down regardless of where i look.
The code i'm using is
public var crosshair : GUITexture;
var playerFlashlight : GameObject;
var player : GameObject;
var layermask : int = 8;
var cameraA : GameObject;
function Start () {
}
function Update () {
var ray : Ray = cameraA.camera.ViewportPointToRay(crosshair.transform.position);
var hit : RaycastHit;
if (Input.GetKeyDown(KeyCode.F))
{
Debug.DrawLine (ray.origin, hit.point, Color.red,2);
}
if(Physics.Raycast(ray, hit, layermask))
{
if (hit.transform.name == "flashlight PICKUP" && Vector3.Distance(transform.position, player.transform.position) <= 3)
{
print ("YOU'RE HITTING IT");
if (Input.GetKeyDown(KeyCode.E))
{
playerFlashlight.GetComponent("LightScript").acquired = true;
playerFlashlight.GetComponent("LightScript").on = true;
Destroy (hit.transform.gameObject);
}
}
}
}
I messed with layermasks to see if it was hitting the capsule but i don't think that's the problem based on the results of the line test.
If anyone has any ideas why this is happening it'd be helpful. Thanks
Answer by robertbu · Nov 02, 2013 at 04:20 AM
You are calling your Debug.DrawLine() before you do the Physics.Raycast(). At that point in the code, 'hit' is uninitialized. In particular (since a RaycastHit is a struct), hit.point will be Vector3.zero. Insert lines 15 - 18 just inside the Raycast() at line 22. Then you will get a true reading of the what is going on:
BTW, I don't see any problems with the Raycast() code. You might want to put the following line at line 22 so that you can see what the Raycast() is hitting. Often with Raycast() problems, the issue is the raycast either hitting things that the programmer did not expect or colliders that are not aligned correctly. The line:
Debug.Log(hit.transform.name+", "+hit.transform.tag);
i see, so my original assumption was right that the ray cast is hitting the players collider because it works fine and is hitting the player when i look down which explains why i have to be at an awkward angle to pick up the flashlight. I don't really understand layer masks, you think you could edit my code so the layermask would work?
the layer the collider is on is 8 which is titled "Player" out of 10 layers that i have. i set the layermask int to 8 but that doesn't seem to be doing anything.
And thank you for helping me with this, i spent all day on this one problem and was having a really hard time with it @robertbu
Layer masks are bit sets. You don't refer to a layer in a layermask by the layer number. The easiest way to fix your code would be to change line 4 to:
var layermask : Layer$$anonymous$$ask;
Then you can go to the inspector and select the specific layer by name. If you really want to assign numbers, then layer 8 will be a value 256 if you are counting from 0. Or you can use:
layermask = 1 << 8;
@robertbu sorry i was at work and then something was happening at home and couldn't get back to you. I tried using the first line of code and it says
BCE0023: No appropriate version of 'UnityEngine.Physics.Raycast' for the argument list '(UnityEngine.Ray, UnityEngine.RaycastHit, UnityEngine.Layer$$anonymous$$ask)' was found.
and the second line doesn't seem to work as everytime i look down at what i'm picking up it tells me i'm looking at the player and not the object
Actually this may account for some of your problems. The error is right that there is no Raycast with that signature. The raycast was treating your 'layermask' as a distance in your original code. Use this form of Raycast() ins$$anonymous$$d:
static function Raycast(ray: Ray, hitInfo: RaycastHit, distance: float, layer$$anonymous$$ask: int): bool;
So your line of code would be:
if(Physics.Raycast(ray, hit, $$anonymous$$athf.Infinity, layermask))
@robertbu tried it at first but it didn't work and then i realized i set it to only raycast the player layer which caused it to only look at the player.
But it works now (i think) and doesn't pick up the player at all.
Thank you so much you've helped me alot