일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- Spring Security
- librecad
- 코딩
- 라즈베리파이
- 운정
- s4a
- oracle
- mysql
- 리브레캐드
- 설정
- Android
- 유니티
- jobtoy
- 아두이노
- MSSQL
- 오라클
- html5
- Unity
- tomcat
- 설치
- 톰캣
- 시작하기
- jsp
- 강좌
- 안드로이드
- 잡토이
- 예제
- 잡토이 메이킹 코딩 학원
- 스크래치
- 파주
Archives
- Today
- Total
랩제이
[유니티 2D] 비행선 추가 본문
Square를 쫓아 오는 비행선을 추가합니다.
비행선의 각도를 보기 위해서 Hierarchy > UI > Text를 추가합니다.
게임오브젝트의 이름을 AngleText로 변경합니다.
비행선이 Square를 보고 회전해서 움직이도록 합니다. 비행선에 Rotate.cs를 추가합니다.
* Rotate.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Rotate : MonoBehaviour
{
public GameObject target;
public GameObject angleText;
void Start()
{
}
void Update()
{
Vector3 dir = target.transform.position - transform.position;
// 각도 계산
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
//Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
//transform.rotation = rotation;
transform.rotation = Quaternion.Euler(0f, 0f, angle);
angleText.GetComponent<Text>().text =
transform.rotation.eulerAngles.z.ToString("F2");
transform.position =
Vector3.MoveTowards(
transform.position,
target.transform.position,
0.01f
);
}
}
비행선에 Roate(Script) 컴포넌트가 추가되고, Target과 AngleText를 연결해 주세요
Target은 Square를 연결합니다.
AngleText는 Canvas > AngleText를 연결합니다.
'유니티' 카테고리의 다른 글
[유니티 2D] 충돌 체크하기 (0) | 2024.03.10 |
---|---|
[유니티 2D] 이미지 반전 (0) | 2024.03.10 |
[유니티 2D] 스마트폰 터치 다운 업을 이용한 움직임 (0) | 2024.03.08 |
[유니티 2D] 화살표 조정기 (1) | 2024.03.08 |
[유니티 2D] 움직이기 로직을 함수로 변경하기 (0) | 2024.03.08 |