- Home /
Angles from Directions
If I want to draw a line straight in front of my game object I can do:
Gizmos.DrawRay(transform.position, transform.forward);
How do I draw a line 15 degrees on each side of this line at the same 10 unit distance?
Answer by DaveA · Apr 13, 2012 at 08:21 AM
May be cheezy, but you could rotate your object 15 degrees, DrawRay, then rotate -30 degrees, DrawRay, then rotate 15 to get back to original rotation.
Hahahah. This actually works. So I'd like to know how to do it mathematically since it's interesting to me, but this will work just fine.
$$anonymous$$aybe use an empty game object attached to the player and this game object is rotating left/right using a cos/sin function to make it go back and forth.
transform.rotation= Vector3(0,A*cos(x)*Time.time,0);
something like this maybe.
I would use this for rotating between between two angles right? Right now I'm using this to raycast.
Your emtpy object would have the raycast script that is sent straight ahead of it Vector3.forward. So if you rotate the object in the local world (guy world not the whole world) the ray is moving from left to right. The whole guy though remains still. It is like the blind guy cane. the blind guy goes straight, the cane sweeps in front.
Answer by fafase · Apr 13, 2012 at 03:33 PM
Ok Think I got it. Create an empty object that you child to your player. the this to the child object.
function Update(){
transform.Rotate(Vector3(0,2*Mathf.Cos(5*Time.time),0));
}
function OnDrawGizmosSelected () {
// Draws a 5 meter long red line in front of the object
Gizmos.color = Color.red;
var direction : Vector3 = transform.TransformDirection (Vector3.forward) * 5;
Gizmos.DrawRay (transform.position, direction);
}
It works for me at least.Now the ray is sweeping in fromt of the guy, you need to define the proper value to make 15 degrees.
Your answer
Follow this Question
Related Questions
Angle Of Ray 2 Answers
Set an angle for bouncing 1 Answer
Rotating character based on joystick angle, at an angle 0 Answers
[2D] Angle to Vector conversion 1 Answer