0
\$\begingroup\$

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

\$\endgroup\$

2 Answers 2

0
\$\begingroup\$

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);
\$\endgroup\$
1
  • \$\begingroup\$ Thats the trick. Thanks. Thanks to other contributors too...I made my question a bit confusing, so it was not clear what was needed! \$\endgroup\$
    – mirzahat
    Aug 26, 2015 at 7:42
0
\$\begingroup\$

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;
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .