유니티 25

[유니티 2D] 아이템 원하는 위치 놓기(2차원 배열 이용)

item 프리팹 파일을 만듭니다. 놓여질 위치의 x, y 좌표를 적어 둡니다. x, y 좌표를 2차원 배열로 만듭니다. * ItemController.cs … public class ItemController : MonoBehaviour { public GameObject itemPrefab; public float span = 2.0f; float temp = 0.0f; int count = 0; int[ , ] map = new int[10, 2] { { 30, 15 }, { 52, 24 }, { 70, 35 }, { 132, 34 }, { 115, 58 }, { 179 ,63 }, { 162, 89 }, { 225, 93 }, { 180, 123 }, { 132, 93 } }; void Start(..

유니티 2024.03.10

[유니티 2D] 칼 정해진 각도로 회전하기

유니티2D에서는 Transform의 Rotation의 z축이 회전입니다. 정해진 각도로 움직이려면 eulerAngles 값을 변경해야 합니다. * SwordController.cs void Update() { if (Input.GetKeyDown(KeyCode.A)) { gameObject.transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, 0f); } if (Input.GetKeyDown(KeyCode.B)) { gameObject.transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, 90f); } if..

유니티 2024.03.10

[유니티 2D] 기어 회전하기

유니티2D에서는 z축이 회전입니다. Transform.Rotate를 이용해서 z의 값을 바꿔줍니다. * GearController.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class GearController : MonoBehaviour { void Start() { } void Update() { // 회전방향을 바꾸려면 100f를 -100f로 바꾸면 됩니다. transform.Rotate(new Vector3(0, 0, 100f * Time.deltaTime)); // transform.Rotate(Vector3.forward * 100f * Time.deltaTime); } }

유니티 2024.03.10

[유니티 2D] 플레이어 따라오기

게임오브젝트가 플레이어를 따라오도록 합니다. * Rotate.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class Rotate : MonoBehaviour { public GameObject target; private void OnCollisionEnter2D(Collision2D collision) { if(collision.gameObject.tag == "Bullet") { transform.position = new Vector3( transform.position.x + 1, 0, 0 ); } } void Start() { } void Update() { Vector3 dir =..

유니티 2024.03.10

[유니티 2D] 미로 - 생명

Hierarcy창에서 UI > Text 선택하고, 게임오브젝트의 이름을 HP로 변경한다. HP 게임오브젝트의 Text>Text를 원하는 값으로 수정합니다. Project > Assets 폴더에 Ghost.prefab 게임오브젝트의 Tag값을 Ghost로 줍니다. Project > Assets 폴더에서 Hp.cs라는 이름의 C#스크립트를 만듭니다. Tag값으로 Ghost란 이름을 가진 게임오브젝트와 만나면 hp가 1점 깍입니다. using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public class Hp : Mon..

유니티 2024.03.10

[유니티 2D] 미로 - Bullet(총알) 만들기

Hierarchy창에서 + 아이콘을 클릭하고 2D Object > Sprites > Circle을 선택합니다. 게임오브젝트의 이름을 Bullet으로 바꿉니다. 총알이 1초 후에 사라지게 하기 위해서 C# 코드를 만듭니다. BulletDestroy.cs C# 코드의 Start() 함수에 Destroy 로직을 사용합니다. * BulletDestroy.cs … public class BulletDestroy : MonoBehaviour { void Start() { Destroy(gameObject, 1); } void Update() { } } Bullet 게임오브젝트에 BulletDestroy.cs C# 코드를 연결합니다. Hierachy창의 Bullet 게임오브젝트을 왼쪽 마우스버튼으로 끌어다가 Asse..

유니티 2024.03.10

[유니티 2D] 미로 - 유령이 플레이어 바라보고 쫓아오기

Assets 폴더의 유령 이미지를 Scene 창에 끌어다 놓습니다. Assets 폴더의 유령 이미지를 Scene 창에 끌어다 놓습니다. Ghost 게임오브젝트가 car 게임오브젝트 쪽으로 움직입니다. Rotate.cs C# 코드를 작성하고 Ghost 게임오브젝트에 연결 시킵니다. * Rotate.cs … public class Rotate : MonoBehaviour { // 전역변수 public GameObject target; void Start() { } void Update() { transform.position = Vector3.MoveTowards(transform.position, target1.transform.position, 0.01f); transform.rotation = Quat..

유니티 2024.03.10

[유니티 2D] 미로 - 카메라가 게임오브젝트 따라가기

카메라가 Car 게임오브젝트를 따라옵니다. CameraMove.cs C#코드를 작성하고, Main Camera 게임오브젝트에 연결시킵니다. target으로 Car 게임오브젝트를 연결합니다. * CarmeraMove.cs … public class CameraMove : MonoBehaviour { public GameObject target; void Start() { } void Update() { transform.position = new Vector3( target.transform.position.x, target.transform.position.y, transform.position.z ); } }

카테고리 없음 2024.03.10