Raycast points to world center

Hi all, got a bizarre one I just can’t figure out. A bit of background to make it make sense:
This is for an AI script, a coroutine in JS that simulates a gun firing. “Spinner” is the child gameObject that had a LookAt script to make it look at it’s target, then the character Slerps to Spinner’s rotation. When tgt is an enemy and the rotation is right, fire() coroutine is called. This much works. I have a raycast in the fire() coroutine, I want it to raycast from muzzle in the direction of spinner, and instantiate an object at the raycast hit.

For some reason though, the raycast goes to 0,0,0 world space, even though it’s miles away. I’ve proved this with Drawline, and the object instantiates correctly at world center. I just can’t figure out why?!? Here’s the coroutine, excuse the lack of formatting, this computer I’m on won’t allow the format button to work from some reason:

function fire()
    var coll : RaycastHit;
    var spinFwd = spinner.transform.TransformDirection(Vector3.forward);
    if(Physics.Raycast(muzzle.position, spinFwd, 20)
        Instantiate(prefab, coll.point, transform.rotation);
        Debug.Drawline(muzzle.position, coll.point);

What’s wrong with this??? It’s driving me mad! 0,0,0 is miles away, certainly not within 20 units, and not in the direction “spinner” is looking. I tried it in Update() with the same results.
Any help greatly appreciated.


OK everyone, I appreciate the help, but I figured it out eventually, and now feel stupid… It’s

if(Physics.Raycast(muzzle.position, spinFwd, coll, 20));

I didn’t put the “Coll” in the Raycast line. I don’t understand why this works, or what it affects, since in the docs first example doesn’t use the RaycastHit variable.
I’ll leave this up in case anyone else has this.

Thanks again guys :slight_smile: