- Home /
How do I convert angle to vector3?
For example I have an object that I want to move at 45 degrees upwards. How do I get the vector coords that I need to push the object at? Thanks
- I want to MOVE the object. Not rotate, MOVE IT. So it moves diagonally 45 degrees. 45 degrees is just an example, I need to find how to convert from angle to vector. I do not want to rotate it, just move.
I think what your after is quaternions but I don't fully understand what you mean by you want to move an object at 45 degrees upwards??? Do you mean you want to rotate an object so it faces up 45 degress than it was before? Do you mean you want it to move in a diagonal direction that is 45 degrees between the x axis and the y axis? What do you mean by you want to push the object? Do you want to push the object in a direction with the player or move the object using keyboard buttons? Please update your question to be more specfic.
Answer by Bunny83 · Mar 26, 2011 at 02:24 PM
Bobadebob is right. Everything that involves rotations/angles you need a Quaternion. Just create a quaternion that rotates 45 upwards and multiply your vector2 with it to rotate the direction.
Use Quaternion.AngleAxis to create your quaternion.
var rot = Quaternion.AngleAxis(45,Vector3.right); // that's a local direction vector that points in forward direction but also 45 upwards. var lDirection = rot * Vector3.forward;
// If you need the direction in world space you need to transform it. var wDirection = transform.TransformDirection(lDirection);
You can of course set up the vector the old, traditional way :D. Just some trigonometry (that's the day your math teacher was talking about)
var angle = 45.0;
var lDirection = Vector3(0, Mathf.sin(Mathf.Deg2Rad * angle), Mathf.cos(Mathf.Deg2Rad * angle));
You're right :D there's only an operator for Quaternion * Vector3. I'll fix that
Hi @Bunny83
I need know one thing in your code, i seen a tutorial that has a code like your code. This is code:
public Vector3 DirFromAngle(float angleInDegree, bool angleIsGlobal)
{
if (!angleIsGlobal)
{
angleInDegree += transform.eulerAngles.y;
}
return new Vector3($$anonymous$$athf.Sin(angleInDegree * $$anonymous$$athf.Deg2Rad), 0, $$anonymous$$athf.Cos(angleInDegree * $$anonymous$$athf.Deg2Rad));
}
I don't understand why used Sin and Cos. and I need know why used Sin for X axis and used Cos for Y axis? Could you please explain me?
Tutorial link: link text
This is a math question more than a program$$anonymous$$g question. The sin and cos functions are part of a branch of math called trigonometry. If you're really interested in gamemaking, it would be worth taking the time to learn trig. $$anonymous$$han Academy has a useful set of lessons here.
Thanks for link. I made some games, but i accept my mathematical is weak. i now Y axis is sine and X axis is Cosine in Circle Trigonometry. i know some formula for change Degree to radians and vice versa. I need know Small things because i'm confused, if possible please answer my some little question. because i'm sure your answers help me to better understand.
1- I think this code want calculate angle in radians. right?
2- I know in Vector3, (x,y,z) must be float numbers so sine and cosine return a float number.Sine and Cosine Used for this reason?
3- why sine used for X axis and cosine for Z axis?
Thanks.