# Unity - Determine direction vector3 from a point in space and an angle in eulers

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:

Code (csharp):

Vector3 direction = Direction (clickPos2 - startPos);


Where the method is:

Code (csharp):

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

if I understood :

"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?"

You need :

dirx = cos(angle);
diry = sin(angle);

• Thats the trick. Thanks. Thanks to other contributors too...I made my question a bit confusing, so it was not clear what was needed! – mirzahat Aug 26 '15 at 7:42

You don't need your Direction() function. It also doesn't return euler angles.

Unity's Vector3 class has all the functions you need.

See the code below.

Vector3 pos1; // start position
Vector3 pos2; // click position
Vector3 moveDistance = 3.0f; // move 3 units in the calculated direction

Vector3 delta = (pos2 - pos1); // delta is the vector difference between pos1 and pos2
float length = delta.magnitude; // distance in a straight line from pos1 to pos2
// direction is delta scaled to a normalized (unit) vector
// A unit vector has all components reduced so that it has a length of 1
Vector3 direction = delta.normalized;

direction.z = 0.0f; // don't move in z direction

// move the object from pos1 towards pos2 by the desired distance
pos1 += direction * moveDistance;