- Home /
RaycastHit Collider Question
function Update () {
var fwd = transform.TransformDirection (Vector3.forward);
var hit: RaycastHit;
if (Physics.Raycast (transform.position, fwd, 100)) {
touchedCol = hit.collider;
}
}
My code with raycasts barely ever works, I'm declaring the touchedCol variable already. I'm not getting errors, it's just that the touchedCol variable isn't returning anything.
Screenshot 2
Could you upload a screenshot of your scene with a Debug.DrawRay where your raycast is going?
Now some weird stuff is happening, I'm going to try and fix that and see if it helps.
Okay I have this script integrated into a modified mouse orbit script, when I do a raycast something weird is happening, the screenshot is in the first post. The raycast length is set to 100 btw.
What object are you trying to hit with your raycast in that screenshot?
At least just the wall, the camera was facing it. No matter how long I make the raycast it stays like that.
Answer by Chimera3D · Jun 26, 2012 at 09:38 PM
I'm stupid...
var touchedCol : Collider;
var length : float;
function Update () {
var fwd = transform.forward;
var hit: RaycastHit;
if (Physics.Raycast (transform.position, fwd, hit, length)) {
touchedCol = hit.collider;
}
Debug.DrawRay (transform.position, fwd * length, Color.green);
}