Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Translate using a magnitude and angle direction

Discussion in 'Scripting' started by svprdga, Aug 21, 2012.

  1. svprdga

    svprdga

    Joined:
    Aug 21, 2012
    Posts:
    14
    Hi,

    I want to know if there is a way to translate an object using a vector, I mean, using a magnitude and an angle direction; not x,y,z coordinates.
    I want it to make a pong game, so the ball movement is creating me trouble since I use normal Vector3 objects to move it.

    Thank you.
     
  2. orb_9

    orb_9

    Joined:
    Feb 10, 2010
    Posts:
    47
    Just normalize your direction vector and multiply it with the magnitude.

    Code (csharp):
    1.  
    2. Vector3 v = myDirVector;
    3. v.Normalize();
    4. v *= magnitude;
    5. transform.Translate(v);
    6.  
     
  3. gfoot

    gfoot

    Joined:
    Jan 5, 2011
    Posts:
    550
    Set one component to the magnitude times the sine of the angle, and another to the magnitude times the cosine of the angle. Which components you choose will affect which plane (XY, XZ, YZ) the ball moves in, and also which direction is considered "zero". You can also make either or both of the values negative to change the zero direction and whether angle increases clockwise or anticlockwise.

    So try:

    Code (csharp):
    1. magnitude * new Vector3(Mathf.Deg2Rad * Mathf.Sin(angle), Mathf.Deg2Rad * Mathf.Cos(angle), 0.0f)
    This moves the ball in the X-Y plane, with zero being straight up (positive Y), and 90 degrees being to the right (positive X).
     
  4. svprdga

    svprdga

    Joined:
    Aug 21, 2012
    Posts:
    14
    gfoot, your solution does not work properly, the translation is not following the correct angle I set.
     
    Last edited: Aug 23, 2012