-유니티에서 사용하는 각도의 단위는 퀀터니언이다.
-쿼터니언은 4차원 복소수로서 세 개의 축을 동시에 회전시켜 짐벌락 현상을 방지한다.
법선 벡터는 두 물체가 충돌한 평면 또는 구면의 접접에서 수직 방향으로 바라보는 벡터
https://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html
Unity - Scripting API: Quaternion.LookRotation
Z axis will be aligned with forward, X axis aligned with cross product between forward and upwards, and Y axis aligned with cross product between Z and X. Returns identity if the magnitude of forward is zero. If forward and upwards are colinear, or if the
docs.unity3d.com
forward- 회전시킬 방향 벡터
upwards- 원하는 방향 벡터를 바라보고 있을때의 "위"를 지정, Vector3.up이 기본값으로 사용됨
=>주어진 방향 벡터를 바라보는 Quaternion을 반환한다.
벽에 충돌시 회전하는 것으로 연습해보았다.
*항상 OnCollisionEnter 쓸때는 두 오브젝트 모두 collider가 있고, 한 오브젝트는 rigidbody가 있어야함을 잊지말자.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class PlayerControl : MonoBehaviour
{
private float distance;
private bool iscollide = false;
[SerializeField] private Transform eye;
// Start is called before the first frame update
void Start()
{
StartCoroutine(CoMoveForward());
}
public void moveForward()
{
Ray ray = new Ray(this.eye.position, this.transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100f))
{
Vector3 moveDir = new(hit.point.x,0, 0);
moveDir += Vector3.forward;
this.transform.Translate(moveDir.normalized * 1f * Time.deltaTime);//이동
}
}
private IEnumerator CoMoveForward()
{
while (true)
{
this.moveForward();
if (iscollide)
{
Debug.Log("벽에 붙음");
break;
}
yield return null;
}
}
private void OnCollisionEnter(Collision collision)
{
this.iscollide = true;
Debug.Log(collision);
if (collision.collider.CompareTag("Wall"))
{
//충돌 지점의 법선 벡터를 구하기 위함
ContactPoint contactPoint = collision.GetContact(0);//첫 번째 충돌 지점의 정보 추출
DrawArrow.ForDebug(contactPoint.point, contactPoint.normal, 5f, Color.magenta, ArrowType.Solid);
Quaternion rotation = Quaternion.LookRotation(contactPoint.normal);
//충돌한 플레이어의 법선 벡터를 쿼터니언 타입으로 변환
//point:위치, normal:법선
this.transform.rotation = rotation;
Debug.LogFormat("normal: {0}, forward: {1}", contactPoint.normal, this.transform.forward);
}
}
}
비트 연산자
- 2진수 : 0, 1
- 10진수 : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
- 16진수 : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
2진수 또는 16진수 → 10진수 : Convert.Int32(, base) 메서드를 사용
10진수 → 2진수 또는 16진수 : Convert.ToString(intVal, base) 메서드사용
보수 연산자 ~: 비트가 1이면 0으로, 0이면 1로 반전
OR 연산자인 | 는 만약 0001 | 0100일 경우 0101을 리턴한다.
AND 연산자인 &는 0011 & 0110일 경우 0010을 리턴한다.
NOT 연산의 ~는 비트를 반전시킨다. ~0010 == 1101 해당 레이어 빼고 전부 선택 시 사용하면 좋다.
XOR 연산인 ^는 대응되는 비트가 서로 다를 때 1을 반환한다. 1101 ^ 0100 == 1001
https://songyeongkim.tistory.com/16
[유니티, C#] layerMask와 비트연산자
유니티의 layerMask는 int형으로 되어 있으며 각 레이어는 비트로 정보를 저장한다. 따라서 layerMask를 사용할 때에 비트 연산을 자주 사용하게 되는데, 그간 비트연산자를 사용할 일이 잘 없었으니
songyeongkim.tistory.com
'C# 기초 복습' 카테고리의 다른 글
이벤트 함수의 실행 순서, ?., ??, ??= 연산자, GetComponentsInChildren/ getchild, activeSelf/activeInHierachy (0) | 2023.08.28 |
---|---|
Unity- Coroutines(코루틴) (0) | 2023.08.08 |
2주차 복습 (0) | 2023.07.31 |
JSON예제, 대리자 복습 (0) | 2023.07.27 |
배열 예제 복습 (0) | 2023.07.25 |