-
[Unity][2D] Rotation 하기프로그래밍/Unity 2021. 10. 25. 20:38728x90
유니티에서 시계 침이 돌아가는 것을 구현하고 싶었던 나..
Summary:
// =============================================================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class arrow_rotation : MonoBehaviour
{
public float rotationSpeed;
public GameObject arrow_center; // Empty Game Object
// Update is called once per frame
void Update()
{
transform.RotateAround(arrow_center.transform.position, Vector3.forward, rotationSpeed*Time.deltaTime);
}
}// =============================================================
첫번째로 찾아본 방법 : Rotate
유니티 공식 문서를 확인해보았다.
https://docs.unity3d.com/ScriptReference/Transform.Rotate.html
Unity - Scripting API: Transform.Rotate
You can specify a rotation in world axes or local axes. World axis rotation uses the coordinate system of the Scene, so when you start rotate a GameObject, its x, y, and z axes are aligned with the x, y, and z world axes. So if you rotate a cube in world s
docs.unity3d.com
Rotation은 Euler angle로 주어진다.
Rotation은 world axes 와 local axes가 있다.
1. World axis: Scene에서 사용되는 좌표계에서 사용 됨.
2. Local axis : Game Object의 좌표계에서 사용됨.
3. 선언
3-1) public void Rotate(Vector3 eulers, Space relativeTo=Space.Self);
3-2) public void Rotate(float xAngle, float yAngle, float zAngle, Space relativeTo=Space.Self);
3-3) public void Rotate(Vector3 axis, float angle, Space relativeTo=Space.Self);
3-4) public void Rotate(Vector3 eulers);
3-5) public void Rotate(float xAngle, float yAngle, float zAngle);
3-6) public void Rotate(Vector3 axis, float angle);
대충 저 중에 하나를 쓰면 된다.
뒤에 relativeTo가 생략되어 있으면? Game object의 relativeTo가 local space로 설정되어 있다. (Space.Self)
이렇게 Script를 짜서 시계를 돌려보려고 하면?! 자기 자신을 중심으로 돈다! Self.World로 바꾸어도 똑같았다.
// =============================================================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class arraow_rotation : MonoBehaviour
{
public float rotationSpeed;
public GameObject arrow_center;
// Update is called once per frame
void Update()
{
transform.Rotate(new Vector3(0, 0, rotationSpeed * 10) *Time.deltaTime, Space.Self);
rotationSpeed += Time.deltaTime * 10;
}
}// =============================================================
두번째로 찾아본 방법 : RotateAround
https://docs.unity3d.com/kr/530/ScriptReference/Transform.RotateAround.html
Unity - 스크립팅 API: Transform.RotateAround
Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. 닫기
docs.unity3d.com
World 좌표에서 point를 중심으로 axis에 대해 angle 만큼 회전한다.
// =============================================================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class arrow_rotation : MonoBehaviour
{
public float rotationSpeed;
public GameObject arrow_center; // Empty Game Object
// Update is called once per frame
void Update()
{
transform.RotateAround(arrow_center.transform.position, Vector3.forward, rotationSpeed*Time.deltaTime);
}
}// =============================================================
Hierarchy에서 Create Empty를 통해 빈 Object를 만들고 빈 Object의 위치를 시계 가운데에 맞추어 준다.
public GameObject arrow_center; 정의해주고
RotateAround의 첫번째 parameter에 arrow_center의 position을 넣어 중심점으로 잡아준다.
Arrow_center에 빈 Object를 넣어주고 돌리면 시계의 중심으로 잘 돌아간다.
? RotateAround의 두번째 parameter 를 뜻하는 axis는 어떻게?!
Vector3의 여러 para들을 넣었을 때 rotationSpeed가 - 기준으로 Vector3.front가 적절했고, +로 한다면 Vector3.back으로 하면 적절했다.
728x90'프로그래밍 > Unity' 카테고리의 다른 글
[Unity][2D] EventSystems를 활용한 UI Control (0) 2021.11.07 [Unity][2D] 카메라 줌 인/줌 아웃 (0) 2021.10.31 [Unity][2D] UI 배치를 위한 Canvas 알아보기 (0) 2021.10.30 [Unity][2D] Grid의 Cell 좌표 구하기 (수정 예정) (0) 2021.10.27 [Unity][2D] 버튼 만들기&연결하기 (0) 2021.10.26