- Home /
Rotating a raycast around the x,z axis
I have a script that will cast a ray forward, but I am having trouble figuring out how to make the ray rotate around 360 degrees.
Here's a part of the script:
public void Cast(){
Ray ray = new Ray(spawn.position,spawn.forward);
RaycastHit hit;
float lightDistance = 20;
if (Physics.Raycast(ray, out hit, lightDistance)){
lightDistance = hit.distance;
}
}
I know that you have to rotate spawn.forward, but I do not know how to do this, as a float cannot rotate a vector 3.
Any help is much appreciated!
Answer by robertbu · Mar 12, 2014 at 07:47 PM
You can rotate a Vector3 using a Quaternion. Let me assume you want to rotate around the transform.up of your spawn object (I don't understand what an x z axis is). You could do something like:
Vector3 v = spawn.forward;
v = Quaternion.AngleAxis(15.0, spawn.up) * v;
This rotates 'v' 15 degrees around the 'up' vector of your spawn object. As for the rotation, you can either do an incremental rotation. That is, rotate by 15 degrees each time, or you can always start with your original vector and increase the amount you rotate (15, 30, 45, ...).
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
slowly rotate a object *need quick fix* 0 Answers
Problems with Raycasting Requirements 0 Answers