- Home /
How can you rotate a raycast on input?
So I'm trying to make raycast script sort of like a sonar whereby if a enemy comes into range the raycast will do a sweep across a number of raycast lines at different angles and back again at around 60fps
For the time being until I can figure out how to do this automatically. I want to find out how it is possible to make a raycast when you put input it will rotate.
An example of this would be the second hand on a clock face. for every time a gear in the clock shifts it moves to the next position.
What I want to do is the same but replace the gear with keyboard input.
the Angle of the raycast would for example start as <----
I then press keyboard key once and the raycast would change rotation to \ I press the keyboard key once again and the raycast would change rotation to | Press it again and it would change rotation to / Finally press is once again and it would change rotate to -->
Then it would go back in the reverse direction on next key input
At the later point in time I want to be able to automate this process and also want to be able to set it so you can create multiple rotation positions for that single raycast.
How would I go about create something like this?
Answer by romatallinn · Jan 05, 2017 at 08:06 AM
Well... A bit confusing explanation of the task. Hope I didn't misunderstand anything.
For raycatsing. Example is given for 3D. If you make a 2D game, then a bit different axis must be used -
public float angle;
//---------
// In this case, final dir is related to player's rotation.
// If you want to relate it to the global axis - use just Vector3.forward instead
Vector3 initDir = transform.forward;
Quaternion angleQ = Quaternion.AngleAxis(angle, Vector3.up); // or use instead of up, the angle's axis you use
Vector3 newVector = angleQ * initDir;
Ray ray = new Ray (transform.position, newVector);
So, if you want to change angle with each input, then just add/decrease some value from the var "angle".
For automating the process, I would use coroutines. Inside a coroutine a loop. You add/decrease some value from the angle and WaitForSeconds if you want to make it change gradually.
Your answer
Follow this Question
Related Questions
Help finding out which side of a cube a raycast hits 1 Answer
Flip over an object (smooth transition) 3 Answers
C# raycast shooting script broken 1 Answer
Raycast problem 1 Answer