유니티 2D 회전



Rotate : Vector3 축에 대해 게임오브젝트를 회전을 시킨다.


GameObject.transform.Rotate(Vector3 value)


public GameObject Level_3;


private void FixedUpdate()

{

Level_3.transform.Rotate(new Vector3(0, 0, 1) * Time.deltaTime * 140);

}



Right : 약간 오른쪽?으로 회전시킬 수 있다.


응용 : 마우스의 위치or터치에 대해 회전 시키는 방법


Vector3 tempTouch;

Vector3 tempVector3;

Vector3 ballDirection2;


GameObject tempObj;


private void FixedUpdate()

{

tempTouch = Input.GetTouch(0);

tempVector3 = mainCamera.ScreenToWorldPoint(tempTouch.position);

tempVector3.z = 0;


ballDirection2 = tempVector3;

ballDirection2.z = tempObj.transform.position.z;


tempObj.transform.right = (ballDirection2 - tempObj.transform.position);

}


1. tempTouch에 터치한 위치 벡터값을 대입하고 월드좌표계 변환, z값을 0으로 바꿈.

2. ballDirection2에 손질한 tempTouch값을 복사하고 z값을 현재오브젝트의 포지션 z값으로 바꿈. (밑에서 계산할 때 사라지게 하기 위함.)

3. ballDirection2 - tempObj.transform.position을 계산하면 tempObj와 터치한 곳을 잇는 벡터가 만들어지고 right을 사용하여그 방향으로 회전시킨다.



'Unity > Tip' 카테고리의 다른 글

유니티 2D 씬 전환  (0) 2018.08.19
유니티 2D 충돌 체크  (0) 2018.08.15
유니티 PlayerPrefs 사용법  (0) 2018.08.11

+ Recent posts