- Home /
Raycast refuses to cast down
Ok, so I'm trying to do what I thought would be a simple piece of code to determine how high a player is from my racetrack. It's simply not working. The rays only ever go to the world origin, and even when I move, they continue to point directly there. Even when I tried getting the distance of the ray, it only ever told me 0. I have the script attached to the game object I want it done with, but the ray simply moves around along behind it.
Here is the code that is controlling the ray cast currently. Any help would be massively appreciated!
var hit : RaycastHit;
var ray : Ray;
Physics.Raycast(transform.position, -Vector3.up, hit);
Debug.Log(hit.distance);
Debug.DrawLine(transform.position, -Vector3.up, Color.red);
Answer by HappyMoo · Jan 02, 2014 at 06:15 PM
It doesn't want to? force it! ;)
you draw your Debug line wrong... the second parameter is the end point, not a direction... you would need to call drawRay for that...
Also... do you have a collider on your track and are you sure your Player is above the track and not exactly on or below(the pivot point at least) it? And it really worked without specifying "out"? Shouldn't have compiled.
if (Physics.Raycast(transform.position, -Vector3.up, out hit))
{
Debug.Log(hit.distance);
Debug.DrawLine(transform.position, hit.point, Color.red);
}
This is UnityScript, so it wouldn't compile with "out" :)
Actually, it isn't compiling with out. I get three errors, telling me it's expecting a closing parenthesis right after out, then expecting token: ), and expecting :, found ';'.
Actually, everything else really helped, as it now works, once I removed the 'out'. Thank you!