- Home /
Question by
Chimera3D · Jun 14, 2012 at 03:30 AM ·
physicsdistancefloatraycasthit
Physics.Raycast Help
Is there reason why this isn't working? I'm not getting anything for the length1 variable. (I have declared a length1 variable elsewhere in the code[this is the only part that matters])
function Update() {
var fwd = transform.forward;
if(Physics.Raycast (transform.position, fwd, 50)){
length1 = Physics.RaycastHit.distance;
}
Debug.DrawRay(transform.position, transform.forward, Color.green, length1);
}
Ok thanks! Now I have another question, how can I get this to work?
function Update() {
var lineRenderer : LineRenderer = GetComponent(LineRenderer);
var fwd = transform.forward;
var hit: RaycastHit; // declare a RaycastHit variable...
// and pass it to the Raycast function:
if(Physics.Raycast(transform.position, fwd, hit, 50)){
length = hit.distance; // then get the result from the hit variable
}else
length = 100;
lineRenderer.positions[1].z = length;
}
Comment
Best Answer
Answer by TehWut · Jun 14, 2012 at 03:37 AM
Hmm I had a problem like this a while back.
I'm guessing that length1 aint working because you never declared the raycast hit in the raycast (did I say that right?) so you would want something more like this.
var hit:RaycastHit;
if (Physics.Raycast(transform.direction,fwd,hit,50))
length1 = hit.distance;
Answer by aldonaletto · Jun 14, 2012 at 03:40 AM
You must pass a reference to a RaycastHit variable to get the results:
function Update() {
var fwd = transform.forward;
var hit: RaycastHit; // declare a RaycastHit variable...
// and pass it to the Raycast function:
if(Physics.Raycast(transform.position, fwd, hit, 50)){
length1 = hit.distance; // then get the result from the hit variable
}
// NOTE: I suppose this instruction should be inside the if:
Debug.DrawRay(transform.position, transform.forward, Color.green, length1);
}