function Update () {
var fwd = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
}
Here I have a raycast on object that will try to detect objects in front of it. If there is an object then take the distance (hit.distance) and change it into a int variable. (In Javascript)
So basically:
var dis : int = hit.distance
However for some reason it represents dis as 0.
Acording to your code, you’re not actually raycasting anything, but only declaring the variables to do so.
Try this :
function Update () {
var rayLength = 10;
var hit : RaycastHit;
var fwd = transform.TransformDirection (Vector3.forward);
if (Physics.Raycast (transform.position, fwd, hit, rayLength)) {
print ("Hit Distance : " + hit.distance);
}
}
You should read this too Unity - Scripting API: Physics.Raycast.