- Home /
Why Isn't My Raycast Working?
I have a script that casts a ray down the center of the screen and it finds the distance of objects and other stuff, it worked fine yesterday, but today it isn't finding distance and I assume it's because it's not even being cast in the first place. If anyone knows what is wrong I would really appreciate help.
#pragma strict
var Distance : int;
var Holding = false;
private var lineTransform : Vector3;
private var startTransform : Vector3;
function Update ()
{
if (Input.GetButtonDown("Fire1"))
{
Path();
}
}
function Path()
{
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
{
if(Holding == false)
{
if (Physics.Raycast (ray, hit, 100))
{
Distance = hit.distance;
if(hit.transform.tag == "PickUp")
{
hit.transform.parent = transform;
hit.rigidbody.useGravity = false;
Holding = true;
lineTransform = hit.point;
}
}
if(Holding == true)
{
if (Physics.Raycast (ray, hit, 100))
{
Distance = hit.distance;
if(hit.transform.tag == "PickUp")
{
hit.transform.parent = null;
hit.rigidbody.useGravity = true;
Holding = false;
lineTransform = hit.point;
}
}
}
}
}
}
Please learn how to format code. I did this for you on this occasion. Questions get rejected normally if the poster hasn't bothered to write a good question.
Just add some Debug.Log()
messages so you can see where the code gets to. Or debug using $$anonymous$$onoDevelop.
Answer by glad · Jul 23, 2014 at 02:34 PM
It could be that your camera is too far from the hittable area. Try to increase a ray distance in "Physics.Raycast (ray, hit, 100)" from 100 to 1000 for example.
Answer by deathripper · Jul 23, 2014 at 03:33 PM
You could also use "Debug.DrawRay" which is nice for Debugging since it Shows you your actual raycast in what ever Color you like.
Answer by thecobe · Jul 23, 2014 at 04:03 PM
I actually got it to work by just restarting and using the same script, so I don't really know what was wrong, but now I can't get it to set the objects parent to null, if anyone knows what's wrong please tell me.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
throw an object by swiping on it 0 Answers
3rd Person camera collision 2 Answers
How To Get Current Animation Name 3 Answers
Not firing a ray (C#) 1 Answer