- Home /
Raycasting from object rotation?
Hello, I have a floppy "Gun" and I want a ray to cast from the tip of it, and while a ray does cast from the tip, it doesn't correlate with the rotation.
Does this ray change rotation as you change the rotation of the "Gun"?
We need to understand your setup and see your code. Typically you would cast from an empty game object positioned at the tip of the gun and a child of the gun. The forward (positive z) side of that empty game object would align with the barrel of the gun. Then you would use 'transform.forward' as your direction for your ray.
The ray does not change rotation as I change the rotation of the gun. I want a ray to cast with the rotation of the gun, not set to a specific axis.
'transform.forward' is the forward direction of an object in world space. It changes as the object is rotated. You can use the transform.position and transform.forward in the Raycast().
Answer by KMKxJOEY1 · Jul 26, 2014 at 11:55 AM
Your raycasting using the camera ray probably, what you need to do is create a ray like this:
public Transform gunBarrel;
Ray ray = new Ray(gunBarrel.position, gunBarrel.forward);
No I really was casting from the object's position but it didn't work. I didn't establish it in a ray though so I'll try that.
Dude seriously? I was also stuck on this for hours! Thanks a bundle!
Your answer
Follow this Question
Related Questions
Raycast is not working properly 2 Answers
rotating ray with player rotation. 2 Answers
Raycast hits and affects all items within the same tag 2 Answers
Strange 'Glitch' while trying to apply the built-in layer "Ignore Raycast" via code 0 Answers
ScreenPointToRay ray not coming through from camera 0 Answers