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

유니티 C# 2018. 1. 29. 16:40

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;



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