Search Unity

Rotation Problem

Discussion in 'Physics' started by henrybatubo, Sep 24, 2018.

  1. henrybatubo

    henrybatubo

    Joined:
    Jun 26, 2018
    Posts:
    1
    Hi how do I make "This " rotate around "That" on the image.

    Thanks
     

    Attached Files:

  2. jvggp

    jvggp

    Joined:
    Jul 8, 2018
    Posts:
    30
    There might be a unity solution, but here a c# solution:

    ///variable in class
    public float alpha=0f; //this is the arc
    public float rotspeed=1.23; //rotation speed

    Start(){
    //pseudocode, you need to get the position the two objects; this code is for case the pc doesnt move in game
    pc=position of center object;
    po=position of circulating object; (initiale position should be the distance you want to have to center object);
    }
    Update(){
    alpha+=Time.deltaTime*rotspeed; //increment arc
    po=pc+Rotation2D(po-pc, alpha); //set new position
    }

    private Vector2 Rotation2D(Vector2 v, float angle)
    {
    float rad = (angle / 180) * Mathf.PI;
    float c = Mathf.Cos(rad);
    float s = Mathf.Sin(rad);

    return new Vector2(c * v.x - s * v.y, s * v.x + c * v.y); //rotation Matrix 2D
    }

    Haven't tested the code, but Rotation2D works proper.
     
    henrybatubo likes this.
  3. jvggp

    jvggp

    Joined:
    Jul 8, 2018
    Posts:
    30
    I have to correct, ther might not be a unity solution, there is a unity solution.
     
    henrybatubo likes this.
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    You could make the white circle a child of the red circle, position the white circle wherever desired, then rotate the red circle.
     
    BoogieD and henrybatubo like this.
  5. BoogieD

    BoogieD

    Joined:
    Jun 8, 2016
    Posts:
    236
    Yep, create a parent/child relationship like Cucci_A wrote above.