I think the issue is in the last if condition: transform.rotation
is aQuaternion
,which is not directly comparable to the (I suppose) scalar value ofweapon.sweepingEdgeRange
. Therefore, you should instead obtain theangle of rotation, around the z-axis.
For example, you could use transform.localEulerAngles,which is
The rotation as Euler angles in degrees relative to the parent transform's rotation.
// We use 'z' component for rotation around z axisfloat currentRotation = transform.localRotation.eulerAngles.z;if (currentRotation < 0){ currentRotation += 360;}if (currentRotation >= weapon.sweepingEdgeRange || currentRotation <= -weapon.sweepingEdgeRange){ Debug.Log("Too far!"); Destroy(gameObject);}