-
[3D] 오브젝트 회전 (Mathf.PI, Mathf.Cos, Mathf.Sin, Quaternion)Unity(유니티) 2022. 7. 8. 10:55
mathf.PI = 파이값, 원주율
mathf.Sin(float f) : z축 좌표 값 구할 때 사용
mathf.Cos(float f) : x 축 좌표 값 구할때 사용
Quaternion : Euler angle은 짐벌락(Gimbal-lock)이라는 문제가 발생하기 때문에 그 한계점을 보완하기 위해 나옴, 2D에서는 큰 문제는 없지만 3D에서는 되도록이면 쿼터니언을 사용
*짐벌락 : x, ,y, z축이 회전하다가 두 개의 축이 겹쳐지는 현상 -> 겹쳐버리면 한 축에 대해서는 계산이 불가능 (정확한 계산 불가)
[MainScript] sin, cos
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CircleItem : MonoBehaviour { public GameObject prefab; public int numberOfObjects = 20; public float radius = 5f; void Start() { for(int i = 0; i < numberOfObjects; i++) { float angle = i * Mathf.PI * 2 / numberOfObjects; float x = Mathf.Cos(angle) * radius; float z = Mathf.Sin(angle) * radius; //포지션 정의 Vector3 pos = transform.position + new Vector3(x, 0.7f, z); float angleDegrees = -angle * Mathf.Rad2Deg; //로테이션 정의 Quaternion rot = Quaternion.Euler(0, angleDegrees, 0); //프리펩 생성 GameObject go = Instantiate(prefab, pos, rot); go.transform.SetParent(gameObject.transform); } } }
[Quaternion Script]
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovement : MonoBehaviour { public float turnSpeed = 20f; Animator m_Animator; Rigidbody m_Rigidbody; AudioSource m_AudioSource; Vector3 m_Movement; Quaternion m_Rotation = Quaternion.identity; void Start() { m_Animator = GetComponent<Animator>(); //GetComponent 메서드가 제네릭 메서드이기 때문에 홑화살괄호가 추가됨 //제네릭 메서드는 2개의 파라미터 세트, 즉 일반 파라미터와 타입 파라미터로 구성된 메서드를 말함 //홑화살괄호 사이에 나열된 파라미터는 타입 파라미터 m_Rigidbody = GetComponent<Rigidbody>(); m_AudioSource = GetComponent<AudioSource>(); } void FixedUpdate() { float horizontal = Input.GetAxis("Horizontal"); float vertical = Input.GetAxis("Vertical"); m_Movement.Set(horizontal, 0f, vertical); m_Movement.Normalize(); //수평 입력 여부 파악 bool hasHorizontalInput = !Mathf.Approximately(horizontal, 0f); //Mathf.Approximately는 2개의 float 파라미터를 갖고 bool 1개를 반환함(2개의 float 값이 유사하면 참, 아니면 거짓을 반환) //수직 입력 여부 파악 bool hasVerticalInput = !Mathf.Approximately(vertical, 0f); //수직, 수평 축에 대한 코드를 하나의 bool로 결합 bool isWalking = hasHorizontalInput || hasVerticalInput; //isWalking 애니메이터 파라미터 설정 m_Animator.SetBool("isWalking", isWalking); if (isWalking) { if (!m_AudioSource.isPlaying) { m_AudioSource.Play(); } } else { m_AudioSource.Stop(); } Vector3 desiredForward = Vector3.RotateTowards(transform.forward, m_Movement, turnSpeed * Time.deltaTime, 0f); m_Rotation = Quaternion.LookRotation(desiredForward); } private void OnAnimatorMove() { m_Rigidbody.MovePosition(m_Rigidbody.position + m_Movement * m_Animator.deltaPosition.magnitude); m_Rigidbody.MoveRotation(m_Rotation); } }
함수설명 출처 : https://backback.tistory.com/435
Unity - Mathf.Cos(), Mathf.Sin() 시야함수 만들기
위 큐브가 적이라고 치고 시선을 보고있는 방향을 구현하였다. 이렇게만 보면 뭔지 이해를 못할 것 같아서 현재 서비스운영중인 게임을 보자면, [가디언 테일즈]게임에서 인베이더(적)들이 보는
backback.tistory.com
쿼터니언 설명 출처 : https://hub1234.tistory.com/21
[유니티] Euler, Quaternion 오일러각 쿼터니언 총 정리
[Unity] Euler, Quaternion 오일러각(짐벌락) 쿼터니언에 대하여! Unity의 Euler 각도는 x,y,z 3개 축을 기준으로 회전시키는 우리가 흔히 알고있는 각도계를 의미한다. 이 각도계를 사용하면 우리 모두 삽
hub1234.tistory.com
'Unity(유니티)' 카테고리의 다른 글
[3D] Random Range Spawn (0) 2022.07.18 [3D]Destroy out of Bounds (0) 2022.07.18 WebGL publishing (0) 2022.05.13 Project Design Document (0) 2022.05.13 [Prototype2] 랜덤 동물 스폰(InvokeRepeating) (0) 2022.05.12