본문 바로가기

저장고

[펌] 2D상에서의 각도를 기반으로 회전값 구하기

반응형

.



[유니티: 방향에 따른 각도]



출처: http://redccoma.tistory.com/110



타겟을 향해 바라봐야한다던가.. 다리같이 뭔가 타겟과 이어주어야 할때 


void Start()

{

// z축 +180은 이미지 방향에 따라 수정하여 적용.

// target1은 자신의 객체가 아닌 비교할 해당 객체.

transform.eulerAngles = new Vector3(0, 0, -getAngle(transform.position.x, transform.position.y, target1.position.x, target1.position.y) + 180.0f);

}


private float getAngle(float x1, float y1, float x2, float y2)

{

        float dx = x2 - x1;

        float dy = y2 - y1;

        

        float rad = Mathf.Atan2(dx, dy);

        float degree = rad * Mathf.Rad2Deg;


        return degree;





. .

반응형