- Home /
How to project an angle from a vector
Answered many thanks for all your help everyone with special thanks to Louis with his single line of code answer :)
I am trying to find a Vector3 point along an angle projected from a Vector. This is the closest I have got and it realy misses the mark. I want to say 'X' degrees from forward facing, in plane 'y' project the 'range' and obtain the 'point' Vector3(x,y,z).
TO TRY AND HELP CLARIFY MY QUESTION Sparkzbarca quote - 'send a ray along an angle and get the point that is RANGE distance away from the origin of the ray?'. I would like the origin angle to be related to the original transforms forward facing. I would like to do it without altering existing transformations.
Please see alucardj picture for absolute clarity (many thanks alucardj ) I would like to find the vector at the green X.
public var scanSpeed : float = 10.0f; public var range : float = 5.0f; public var speed : float = 10.0f; public var rotationSpeed : float = 10.0f;
private var shadowSelf : Transform;
function Start () { shadowSelf = transform; }
function Update () { var rotAngle : Vector3; //Show forward direction Debug.DrawLine(shadowSelf.position, shadowSelf.forward (range + 1), Color.green); for (var x : int = 0; x < 90; x = x + 1) { // Sweep scan beam left n right 1-90 degrees rotAngle = shadowSelf.TransformDirection(Quaternion.AngleAxis(x, Vector3.up).eulerAngles); Debug.DrawLine(shadowSelf.position, rotAngle range, Color.red); rotAngle = shadowSelf.TransformDirection(Quaternion.AngleAxis(-x, Vector3.up).eulerAngles); Debug.DrawLine(shadowSelf.position, rotAngle * range, Color.magenta); DebugPrintOut(rotAngle, "rotAngle"); } }
function DebugPrintOut(vec3 : Vector3, prnStr : String) { Debug.Log(prnStr + " = Vector3(x-" + vec3.x + ",y-" + vec3.y + ",z-" + vec3.z + ")"); }
I can im certain help with this but just to make sure you want to basically want to
send a ray along an angle and get the point that is RANGE distance away from the origin of the ray?
I know you were trying to be general in your description but try being specific. Say what you want your game object to do without using technical stuff just in general I want my player to find any object with 45 degrees left or right of its view or range finding or something.
I can tell you for example how to create a plane that goes up and down so far out in front of you and then the vector point on that plane that a ray crosses it.
I can tell you how to find the point in space that is x distance along a ray.
Or is your problem maybe finding the directional vector that is x degrees from forward itself? im just not sure what you want exactly.
i know people hate pictures but honestly when it comes to geometric problems like this
DRAW IT OUT
makes it easier for other people to understand and occasionally even helps you figure it out but im lost
Sparkzbarca quote - 'send a ray along an angle and get the point that is RANGE distance away from the origin of the ray?'. I would like the origin angle to be related to the original transforms forward facing. I would like to do it without altering existing transformations.
I, like others want to help but am having trouble understanding the idea. I did this in paint, is this what you mean ?
Answer by Loius · Nov 20, 2012 at 09:00 PM
To rotate a vector:
Quaternion * Vector3 = rotated vector
So.
position = (Quaternion.Euler( angleX, angleY, angleZ ) transform.forward ).normalized range;
Thank you Louis - your single line of code is the answer Ive just run it up in some code - I would vote for you but the system wont let me.
You can only 'vote' (thumb) when you have enough karma. To mark an answer as accepted :
On the left-hand-side of the Answer box , there are the following icons :
Thumb Up
Number (of votes)
Thumb Down
A Tick/Check $$anonymous$$ark
If an answer worked for you, click on the 'Tick/Check mark', the answer should now be highlighted in green and marked as accepted.
*Loius. :)
Welcome! Glad to help. Watch out for Quaternions - if you don't have a strong math background, don't try to figure out how they work. Just use the code snippets in the scripting reference and assume they're made of magic.
Answer by sparkzbarca · Nov 20, 2012 at 04:05 PM
Ray myray;
myray.origin = gameobject.transform.position;
next we need to take the forward direction and add the additional angle.
A directional vector is really the slope of a line. The slope of an angle is defined as its rise over its run, or its hypotenuse over its base. The hypotenuse over base is a common trigometric identity called its tangent.
The tangent of an angle is the angle slopes.
The slope of an angle added to forward should give us a vector which is angle degrees off center from forward.
myray.direction = gameobject.transform.forward + mathf.tan(angle);
ray.GetPoint(distance);
that gets you a point at set distance at a set angle.
Thanks dude but there is a problem .... myray.direction = gameobject.transform.forward + mathf.tan(angle); is not valid as 'gameobject.transform.forward' is Vector3 and 'mathf.tan(angle)' is float which causes and error. Please could you fix as this would be exactly the answer Im looking for if it works.
sorry to get 2 numbers its technically
vector2(cosine(angle), sin(angle))
and almost forgot convert your angle to radians.
180/pie * degrees == degrees in radians
so degrees = degrees_off
radians = 180/pie * degrees
directional_vector = transform.forward + vector2(cos(radians),sin(radians));
ray.direction = directional_vector;
ray.getpoint(distance);
roughly
Answer by SaxonHammer · Nov 20, 2012 at 10:47 PM
This is the answer guys and you dont have to create any objects to get there
#pragma strict
public var range : float = 5.0f;
function Update () { var targetPos : Vector3; // REQUIRED answer position for (var x : int = 0; x < 90; x = x + 1) // Angle from forward { // the fantastic single line of code provided by Loius targetPos = (Quaternion.Euler( 0, x, 0 ) transform.forward ).normalized range; // visual output Debug.DrawLine(transform.position, targetPos, Color.green); // Print to console window DebugPrintOut(targetPos,"targetPos"); } }
function DebugPrintOut(vec3 : Vector3, prnStr : String) { Debug.Log(prnStr + " = Vector3(x:" + vec3.x + ",y:" + vec3.y + ",z:" + vec3.z + ")"); }