Hello
I have a ball positioned as Vector3(x, x, x).
I was able to get the direction of the ball to the mouse clicked position with:
Vector3 direction = Direction (clickPos2 - startPos);
Where the method is:
public Vector3 Direction(Vector3 position) {
return new Vector3(position.x / Length(position.x, position.y), position.y / Length(position.x, position.y), position.z);
}
Now I have an angle in eulers and I want that the ball goes to the direction of the angle.
But how I calculate it’
The game is played in 2d so actually only x and y is needed, z stays the same.
Mirza
No need for that function - mathematics will set you free:
Vector3 direction = clickPos2 - startPos;
float angle = Mathf.Atan2(direction.y, direction.x);
float xVelocity = Mathf.cos(angle) * speed;
float yVelocity = Mathf.sin(angle) * speed;
Hello,
thanks for answer.
I think you misunderstood the question a bit:
I have a point x, y
I have an angle of 45 in eulers.
How do I get the direction vector of the point to the angle?
Mirza
Kiwasi
August 25, 2015, 9:42pm
4
Picture? Pictures are essential to communicating geometry questions.
So want the ball to roll in tha direction. I hav the angle and the ball position,
but dont know how to create a direction vector.
Mirza
If the angle is always relative to the 2d world axis and not relative to the direction of movement, just take the second part of @GroZZleR 's code. Cos(angle) will tell you how far to move in x-direction, sin(angle) how far in y-direction. The resulting direction vector will have the magnitude of 1. That’s why he also multiplied it by speed.