2D 회전
```
Vector2 targetPosition = target.position;
Vector2 myPosition = transform.position;
Vector2 direction = (targetPosition - myPosition).nomalize
float angle = Mathf.Atan2(direction.y,direction.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.Eluler(0,0,angle);
transform.rotation = rotation;
```
강제 이동
```
transform.position = transform.position + direction * Time.deltaTime;
//비슷한 형태
transform.transform.Translate(direction * Time.deltaTime,Space.World);
//Space.Self or Space.World 등으로 자신 또는 월드 좌표를 기준으로 이동이 가능하다.
```
//단점 가끔 벽을 뚫거나 떨림 현상이 심하다.
//장점 비용이 가장 저렴하다.
지정 위치 이동
```
// 정해진 위치로 이동
Vector3 movePosition = Vector3.MoveTowords(transform.position,target.position,Time.deltaTime * moveSpeed);
transform.position = movePosition;
```
//단점 물리적인 상호작용과 엇박자를 일으킬 수 있음
//장점 비용이 저렴하면서 강제 이동 보다 깔끔한 형태로 동작한다.
물리 좌표 이동
```
//RigidBody2D rb = GetComponent<Rigidbody2D>();
rb.MovePosition(rb.position + direction * moveSpeed * Time.fixedDeltaTime);
```
물리 속도
```
rb.velocity = direction * moveSpeed;
```
//단점 밀려나는 효과를(KnuckBack) 사용하려면 예외 처리를 해줘야 한다.
//장점 이것을 이동이 아닌 특정 밀려나는 효과에 적용하면 물리적인 효과를 최대화 시킬 수 있다.
물리 가속도
```
rb.AddForce(direction,ForceMode2D.Impulse);
```
// 이것은 속도가 점점 중첩 증가 되는 것을 표현 할 수 있다.
// ex) 슈퍼마리오 의 미끄러지는 형태의 이동
// 물론 이것 말고도 다른 기믹이 존재한다.
// 키워드 : Physics Material 2D
반사 각 구현하기
```
// 법선 벡터: 입사 벡터의 수직 벡터 (90도 회전)
Vector2 normal = new Vector2(-direction.y, direction.x);
// 반사 벡터 계산
Vector2 reflectionVector = direction - 2 * Vector2.Dot(direction, normal) * normal;
// 반사 벡터를 기반으로 새로운 이동 방향 설정
rb.velocity = reflectionVector * rb.velocity.magnitude;
```
// 당구 혹은 튕기는 총알 등 구현 시
해당 좌표를 기준으로 주변 물체 찾기
```
float range =5f;
Collider2D[] coll = Physics2D.OverlapCircleAll(transform.position,range);
//Collider2D[] coll = Physics2D.OverlapCircleAll(transform.position,range,LayerMask);
//LayerMask를 추가하여 특정 Layer에 해당하는 물체만 찾을 수 있다.
```
//폭탄 같이 범위 피해를 주거나 해당 범위에 특정 효과를 부여할 때 사용하면 좋다.
'Unity' 카테고리의 다른 글
Visual Studio 비쥬얼 스튜디오 에서 자동 완성으로 AI에게 지배를 당하고 있다면 (0) | 2024.10.25 |
---|---|
유니티 Visual Script Editor 연동 실패 (1) | 2024.10.23 |
Unity WebGL을 사용하여 GitHub에 업로드 해보자! (0) | 2024.10.22 |
Unity 2D 유용한 구문 (0) | 2024.10.21 |
Addressable 활용하여 App 내부에서 업데이트 해보자 (2) | 2024.10.21 |
댓글