Search Unity

Problem with Trigonometry

Discussion in 'Scripting' started by Mai Kel, Sep 2, 2014.

  1. Mai Kel

    Mai Kel

    Joined:
    Nov 5, 2013
    Posts:
    14
    Hello, how are you?

    I have an issue with this code and I am stuck with it:

    This is a code piece that I used to use back in the game maker days. But in unity doesn't quite respond in the same way.

    Code (CSharp):
    1. for (int i = 1; i<=360; i+=120) {
    2.  
    3.                                 GameObject option = (GameObject)Instantiate (circular);
    4.                                 float xD = (Mathf.Cos (i));
    5.                                 float yD = (Mathf.Sin (i));
    6.  
    7.                                 Vector3 pos = new Vector3 (transform.position.x + len * xD, transform.position.y + len * yD, 0);
    8.                                 option.transform.position = pos;
    9.                                 option.transform.parent = transform;
    10.                                 abec.Add (option);
    11.  
    12.                         }
    I expected to have 3 equidistant option around my transform. But instead the 3 of them appear in the upper part.

    Any help?

    Thank you!
     
  2. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    Aren't you looking for arc sine/cosine? Also, those functions us radians, not degrees.
     
  3. Mai Kel

    Mai Kel

    Joined:
    Nov 5, 2013
    Posts:
    14
    Not really, I am looking for the coordinate given by x=x+length*cosine(iterator) / y=y+length*sine(iterator) so for 360 degrees / 3 would set one option at 1º, 120º and 240º...

    I tried multiplying for radtodeg but keeps mostly the same...

    am I doing something wrong at: ??

    Code (CSharp):
    1. float xD = Mathf.Cos (i)*Mathf.Rad2Deg;
    2.                                 float yD = Mathf.Sin (i)*Mathf.Rad2Deg;
    3.                                 Vector3 pos = new Vector3 (transform.position.x + len * xD, transform.position.y + len * yD, 0);
     
  4. Mai Kel

    Mai Kel

    Joined:
    Nov 5, 2013
    Posts:
    14
    sorted.

    Code (CSharp):
    1.                                 float xD = Mathf.Cos ((float)i * Mathf.Deg2Rad);
    2.                                 float yD = Mathf.Sin ((float)i * Mathf.Deg2Rad);
    3.  
    4.                                 print (xD + "  " + yD);
    5.  
    6.                                 Vector3 pos = new Vector3 (transform.position.x + len * xD / 100, transform.position.y + len * yD / 100, 0);
    Had to be given in radians. Remember kids! give them radians