- Home /
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.
Regards Ian
Debug where you are casting the ray :
var rayDistance : float = 1000.0;
var spinFwd = spinner.transform.TransformDirection(Vector3.forward);
Debug.DrawRay( muzzle.position, spinFwd * rayDistance, Color.yellow );
if ( Physics....
you may also want to put a time in for how long the DrawRay is visible :
Debug.DrawRay( muzzle.position, spinFwd * rayDistance, Color.yellow, 0.5 );
http://docs.unity3d.com/Documentation/ScriptReference/Debug.DrawRay.html
Edit : in general, why do some people use TransformDirection?
Answer by husbandofemily · Apr 22, 2013 at 07:53 PM
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 :-)
Ian
$$anonymous$$arked as accepted to close the question. Glad you got it figured out.
As for why adding 'coll' causes it to work: When you create the coll variable, you're creating a new struct. By default, all its values with be whatever the default is for their type (e.g. the point will be (0,0,0), the distance will be 0, etc.). When you pass Coll to the overloaded Raycast method, it fills in the Coll struct with the information from the collision. Only if you do so will Coll have real data.
If you're not familiar with structs and overloading, take a moment to google them - they're quite useful terms to know.
Your answer
![](https://koobas.hobune.stream/wayback/20220613100833im_/https://answers.unity.com/themes/thub/images/avi.jpg)