Can't get Debug.DrawRay() to draw anything

I know why… sort of, but I don’t know how to fix it.

If I have gun called turret, which rotates. At the tip of the gun barrel is spawnPoint.

When the player presses Fire, I want to use a ray to check for collisions with other objects (rather than having a physical object move forwards until it hits something).

The problem arises because turret.transform.rotation (which I need to be the ray’s direction) is a quaternion. Much googling has suggested that Quaternion.eulerAngles is the very thing I need. Except when I do this I get no error, but then Debug.DrawRay(ray.origin, ray.direction * bulletRange) results in nothing being drawn. bulletRange, by the way, is 10.

If I hack the direction parameter with New Vector3(0,0,1), I get a fixed line. So it seems to me that eulerAngles is not doing what I’m being told that it does.

My code after much hacking around is a horrible mess, hence I haven’t posted any. Just wondering if anyone can give me any pointers on this.

Thanks.

[edit] Info source: convert quaternion to vector3? - Unity Engine - Unity Discussions

EulerAngles is a representation of the rotation of the object around the three different axes - not an arrow pointing in the direction the object is facing. If you want to draw a line directly forward, replace turrent.transfrom.rotation.eulerangles with turret.transform.forward.

I don’t want to draw a line directly forward. I want to create a ray that moves to the position of the spawnPoint, and adopts the rotation of the turret. It’s the rotation bit I cannot get my head around as I can’t see how to get from a quaternion to a vector3. Maybe I’m just going about it all the wrong way.

Assuming you want the Ray to follow the Turret:

It’s hard to determine a good solution for this without understanding your model structure, but let’s just assume that your model is a turret that is laying along the Z-axis with the muzzle facing in positive Z (so basically, a cylinder along the Z-axis).

Assuming this is how your model is oriented, you can use transform.forward to get the current direction of the muzzel because forward represents the +Z axis and transform.forward is a shorthand for the following code:

Vector3 currentFacing = transform.localRotation * Vector3.forward;

Basically, if you know the orientation of your muzzle (which direction its facing when its rotation is (0, 0, 0)), you can use transform.localRotation * normalMuzzleFacing to get its current facing direction.

If you want to be in front of the muzzle, you just need to know what the distance of the muzzle is since you already have the direction of the muzzle.

From there, you’d do something like the following to get the Ray you need:

-Calculate the new muzzle direction using the method I described above
-Use the transform.position + (muzzleDistance * muzzleDirection.normalize) for the origin point of the ray from the muzzle
-Use muzzleDirection as the direction of the ray

Note: What Erich was referring to in the thread that you supplied was converting the angular-representation of the Quaternion’s orientation to a Vector3 in euler angle form (because the thread was asking how to convert a Quaternion to a Vector3). He wasn’t describing how to rotate a Vector3 value about a Quaternion, which is what I think you need to do for this particular problem.

Assuming you want the Turret to follow the Ray:

You can assign directions to transform.up, transform.forward, and transform.right. Those represent the local axis of the Transform. I haven’t used ‘Look At’, but that may be an option too. It depends on your model’s orientation, because if your muzzle doesn’t lie along an axis, the problem will be slightly more complicated but still doable.