using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.SearchService;
public class coTest : MonoBehaviour
{
void Start()
{
//StartCoroutine(this.Sol1());
//StartCoroutine(this.Sol2());
}
void Update()
{
}
//1. 주어진 시간(예: 5초) 동안 특정 작업을 반복 실행하고 그 후에 작업을 중지하는 코루틴을 작성하세요.
//IEnumerator Sol1()
//{
// Debug.LogFormat("시작"); // 처음에 0초 출력
// yield return new WaitForSeconds(1); // 1초 대기
// for (int i = 1; i <= 5; i++)
// {
// Debug.LogFormat("{0}초 경과", i);
// yield return new WaitForSeconds(1); // 각 반복마다 1초씩 대기
// }
// Debug.Log("정지");
//}
//2. 리스트에 있는 항목을 하나씩 출력하고 각 항목을 출력한 후에 잠시 대기하는 코루틴을 만들어보세요.
//IEnumerator Sol2()
//{
// string[] items = { "cap", "clothes", "pants", "shoes" };
// foreach (string item in items)
// {
// Debug.Log(item);
// yield return new WaitForSeconds(1);
// }
// Debug.Log("모든 아이템 출력 완료");
//}
//3.플레이어가 특정 키를 누를 때까지 게임을 일시 중지하고, 그 키를 누르면 다시 게임을 계속하는 코루틴을 작성하세요.
}
using System.Collections;
using UnityEngine;
public class GameLogic : MonoBehaviour
{
private bool gamePaused = false;
void Start()
{
StartCoroutine(GameLoop());
}
//3. 플레이어가 특정 키를 누를 때까지 게임을 일시 중지하고, 그 키를 누르면 다시 게임을 계속하는 코루틴을 작성하세요.
IEnumerator GameLoop()
{
while (true)
{
if (!gamePaused)
{
// 게임이 진행 중인 경우 여기에 원하는 게임 로직을 추가
Debug.Log("게임 진행 중");
}
// 플레이어가 스페이스바를 누르면 일시 중지 상태를 변경
if (Input.GetKeyDown(KeyCode.Space))
{
gamePaused = !gamePaused;
if (gamePaused)
{
Debug.Log("게임 일시 중지");
Time.timeScale = 0f; // 게임 시간 정지
}
else
{
Debug.Log("게임 재개");
Time.timeScale = 1f; // 게임 시간 재개
}
}
yield return null;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tank : MonoBehaviour
{
public enum Mode
{
TankMode,
SiegeMode
}
void Start()
{
}
void Update()
{
if (Input.GetMouseButtonDown(1))
{
Vector3 mousePosScreen = Input.mousePosition;
Debug.Log(mousePosScreen); // 캔버스의 크기는 좌측하단 0*0 부터 1920 * 1080이지만 추가로 월드좌표가찍힘
}
}
}
이렇게 하면 Local position의 좌표가 찍히는데 이를 월드좌표로 바꿔줄 것이다.
월드 좌표로 바꾸어 보았다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tank : MonoBehaviour
{
public enum Mode
{
TankMode,
SiegeMode
}
void Start()
{
}
void Update()
{
if (Input.GetMouseButtonDown(1))
{
Vector3 mousePosScreen = Input.mousePosition;
//Debug.Log(mousePosScreen); // 캔버스의 크기는 좌측하단 0*0 부터 1920 * 1080이지만 추가로 월드좌표가찍힘
Vector2 mousePosWorld = Camera.main.ScreenToWorldPoint(mousePosScreen);
Debug.Log(mousePosWorld); // (0.00, 1,00)
}
}
}
좌하단 -> 중앙 -> 오른쪽위 순으로 나온 좌표이고
탱크의 위치는 0,0,0 으로 초기화
카메라는 탱크의 위치에 따라가게 Ctrl + Shift + F로 지정하였다.
탱크를 클릭한 위치로 이동하게 하였다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tank : MonoBehaviour
{
public float Speed = 3f;
public enum Mode
{
TankMode,
SiegeMode
}
void Start()
{
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 mousePosScreen = Input.mousePosition;
//Debug.Log(mousePosScreen); // 캔버스의 크기는 좌측하단 0*0 부터 1920 * 1080이지만 추가로 월드좌표가찍힘
Vector2 mousePosWorld = Camera.main.ScreenToWorldPoint(mousePosScreen);
Debug.Log(mousePosWorld); // (0.00, 1,00)
}
if (Input.GetMouseButton(1))
{
Vector3 mousePosScreen = Input.mousePosition;
Vector2 mousePosWorld = Camera.main.ScreenToWorldPoint(mousePosScreen);
this.transform.position = Vector3.MoveTowards(this.transform.position, mousePosWorld, Speed * Time.deltaTime);
}
}
}
this.transform.position: 현재 객체(탱크)의 위치를 나타냅니다.
mousePosWorld: 마우스의 현재 위치를 나타냅니다.
Speed * Time.deltaTime: 탱크가 이동할 속도를 결정합니다. Time.deltaTime은 이전 프레임부터 현재 프레임까지의 경과 시간을 나타내며, 이를 사용하여 프레임 레이트에 관계없이 일정한 속도로 이동할 수 있습니다.
스타크래프트 마우스를 클릭하고 있어야 되는 것이 아닌 한번 누르면 이동해야 하고 중간에 경로를 변경할 수 있어야 한다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tank : MonoBehaviour
{
public float Speed = 3f;
public Vector3 targetPosition = Vector3.zero; // 클릭한 위치를 저장할 변수
void Update()
{
if (Input.GetMouseButtonDown(1)) // 오른쪽 마우스 버튼을 클릭했을 때
{
// 마우스 클릭한 위치를 저장
Vector3 mousePosScreen = Input.mousePosition;
targetPosition = Camera.main.ScreenToWorldPoint(mousePosScreen);
//targetPosition.z = 0f; // 2D 게임에서 z 축 값은 0으로 고정
Debug.Log("New target position: " + targetPosition);
}
// 탱크가 저장된 위치로 이동
if (targetPosition != Vector3.zero)
{
transform.position = Vector3.MoveTowards(transform.position, targetPosition, Speed * Time.deltaTime);
// 탱크가 목표 위치에 도달하면 저장된 위치 초기화
if (this.transform.position == targetPosition)
{
targetPosition = Vector3.zero;
}
}
}
}
타켓(클릭된)좌표의 벡터를 0으로 초기화 => Vector3.zero
이동좌표를 정할 때
나중에 코드가 길어지면 업데이트에서 처리할일이 많아서 느려질 수 있으므로 코루틴을 사용했다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tank : MonoBehaviour
{
public float Speed = 3f;
public Vector3 targetPosition = Vector3.zero; // 클릭한 위치를 저장할 변수
private void Start()
{
}
void Update()
{
if (Input.GetMouseButtonDown(1)) // 오른쪽 마우스 버튼을 클릭했을 때
{
StartCoroutine(CoMove()); // 코루틴을 실행합니다.
}
}
IEnumerator CoMove()
{
// 마우스 클릭한 위치를 저장
Vector3 mousePosScreen = Input.mousePosition;
targetPosition = Camera.main.ScreenToWorldPoint(mousePosScreen);
//targetPosition.z = 0f; // 2D 게임에서 z 축 값은 0으로 고정
Debug.Log("New target position: " + targetPosition);
// 탱크가 저장된 위치로 이동
while ((targetPosition != Vector3.zero))
{
transform.position = Vector3.MoveTowards(transform.position, targetPosition, Speed * Time.deltaTime);
// 탱크가 목표 위치에 도달하면 저장된 위치 초기화
if (this.transform.position == targetPosition)
{
targetPosition = Vector3.zero;
}
yield return null;
}
}
}
하지만 실행을 해보니 클릭을 할 수록 빨라졌고 코루틴을 중지하는 것이 필요하다고 판단하였다.
멤버변수에 현재 실행중인 코루틴을 저장하기 위해 선언하였고
코루틴안에서 실행중이면 정지하도록 하고
다시 코루틴을 시작하게 하였다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tank : MonoBehaviour
{
public float Speed = 3f;
public Vector3 targetPosition = Vector3.zero; // 클릭한 위치를 저장할 변수
private Coroutine moveCoroutine; // 현재 실행 중인 코루틴을 저장하기 위한 변수
private void Start()
{
}
void Update()
{
if (Input.GetMouseButtonDown(1)) // 오른쪽 마우스 버튼을 클릭했을 때
{
// 기존에 실행 중인 코루틴이 있으면 중지시킴
if (moveCoroutine != null)
{
StopCoroutine(moveCoroutine);
}
// 새로운 코루틴 시작
moveCoroutine = StartCoroutine(CoMove()); // 코루틴을 실행합니다.
}
}
IEnumerator CoMove()
{
// 마우스 클릭한 위치를 저장
Vector3 mousePosScreen = Input.mousePosition;
targetPosition = Camera.main.ScreenToWorldPoint(mousePosScreen);
//targetPosition.z = 0f; // 2D 게임에서 z 축 값은 0으로 고정
Debug.Log("New target position: " + targetPosition);
// 탱크가 저장된 위치로 이동
while ((targetPosition != Vector3.zero))
{
transform.position = Vector3.MoveTowards(transform.position, targetPosition, Speed * Time.deltaTime);
// 탱크가 목표 위치에 도달하면 저장된 위치 초기화
if (this.transform.position == targetPosition)
{
targetPosition = Vector3.zero;
}
yield return null;
}
}
}
코드를 수정하기 전에도 있었던 문제 같은데 위치에 도달하면 Tank가 사라지는 것을 발견
scene에는 있는데 game scene에서 사라지는 이유를 찾다가
layer in order로 맵뒤에 있는건 아닌지 확인도 하였지만
알고보니, 2D인데 클릭한 좌표가 z축이 입력되서 카메라 시점이 같이 움직였기에 시야 밖에서 사라진 것이었다.
z좌표를 0 으로 초기화하는 변수를 지정하고 인자로 넣어주었더니 문제 없이 실행되었다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tank : MonoBehaviour
{
public float Speed = 3f;
private Vector3 targetPosition = Vector3.zero; // 클릭한 위치를 저장할 변수
private Coroutine moveCoroutine; // 현재 실행 중인 코루틴을 저장하기 위한 변수
void Update()
{
if (Input.GetMouseButtonDown(1)) // 오른쪽 마우스 버튼을 클릭했을 때
{
// 기존에 실행 중인 코루틴이 있으면 중지시킴
if (moveCoroutine != null)
{
StopCoroutine(moveCoroutine);
}
// 새로운 코루틴 시작
moveCoroutine = StartCoroutine(CoMove()); // 코루틴을 실행합니다.
}
}
IEnumerator CoMove()
{
// 마우스 클릭한 위치를 저장
Vector3 mousePosScreen = Input.mousePosition;
targetPosition = Camera.main.ScreenToWorldPoint(mousePosScreen);
Debug.Log("New target position: " + targetPosition);
// 탱크가 저장된 위치로 이동
while (Vector3.Distance(transform.position, targetPosition) > 0.01f)
{
Vector3 movePos = new Vector3(targetPosition.x, targetPosition.y, 0);
transform.position = Vector3.MoveTowards(transform.position, movePos, Speed * Time.deltaTime);
yield return null;
}
// 탱크가 목표 위치에 도달하면 저장된 위치 초기화
targetPosition = Vector3.zero;
}
}
더 해야할 부분 :
아직 미완성인 부분 o버튼 누를경우 이미지 시즈모드로 변경 다시 o누르면 탱크모드
+ 이동중에 o를 누르면 그 자리에서 즉시 이미지 변경
우선 O키를 눌러서 콘솔에 찍어보았고
움직이는 중간에 계속 콘솔에 찍혔다.
이 점에 대해 구현을 한다면 O를 누르면 시즈모드가 멈추고 이미지가 변경 되어야 할 것이다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tank : MonoBehaviour
{
public float Speed = 3f;
private Vector3 targetPosition = Vector3.zero; // 클릭한 위치를 저장할 변수
private Coroutine moveCoroutine; // 현재 실행 중인 코루틴을 저장하기 위한 변수
void Update()
{
if (Input.GetMouseButtonDown(1)) // 오른쪽 마우스 버튼을 클릭했을 때
{
// 기존에 실행 중인 코루틴이 있으면 중지시킴
if (moveCoroutine != null)
{
StopCoroutine(moveCoroutine);
}
// 새로운 코루틴 시작
moveCoroutine = StartCoroutine(CoMove()); // 코루틴을 실행합니다.
}
if (Input.GetKeyDown(KeyCode.O))
{
Debug.Log("시즈모드 전환");
}
}
IEnumerator CoMove()
{
// 마우스 클릭한 위치를 저장
Vector3 mousePosScreen = Input.mousePosition;
targetPosition = Camera.main.ScreenToWorldPoint(mousePosScreen);
Debug.Log("New target position: " + targetPosition);
// 탱크가 저장된 위치로 이동
while (Vector3.Distance(transform.position, targetPosition) > 0.01f)
{
Vector3 movePos = new Vector3(targetPosition.x, targetPosition.y, 0);
transform.position = Vector3.MoveTowards(transform.position, movePos, Speed * Time.deltaTime);
yield return null;
}
// 탱크가 목표 위치에 도달하면 저장된 위치 초기화
targetPosition = Vector3.zero;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BionicUnit
{
public enum BionicUnitType
{
Marine,
Medic
}
public BionicUnitType type;
public string name;
public BionicUnit(BionicUnitType type, string name)
{
this.type = type;
this.name = name;
}
}
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class DropShip : MonoBehaviour
{
//마린과 메딕을 관리할 리스트
public List<BionicUnit> units = new List<BionicUnit>();
//최대로 태울 수 있는 인원의 수
public const int CAPACITY = 8;
void Start()
{
//생성자를 만든다
BionicUnit medic = new BionicUnit(BionicUnit.BionicUnitType.Medic, "메딕");
BionicUnit marine1 = new BionicUnit(BionicUnit.BionicUnitType.Marine, "마린1");
BionicUnit marine2 = new BionicUnit(BionicUnit.BionicUnitType.Marine, "마린2");
BionicUnit marine3 = new BionicUnit(BionicUnit.BionicUnitType.Marine, "마린3");
BionicUnit marine4 = new BionicUnit(BionicUnit.BionicUnitType.Marine, "마린4");
BionicUnit marine5 = new BionicUnit(BionicUnit.BionicUnitType.Marine, "마린5");
BionicUnit marine6 = new BionicUnit(BionicUnit.BionicUnitType.Marine, "마린6");
BionicUnit marine7 = new BionicUnit(BionicUnit.BionicUnitType.Marine, "마린7");
BionicUnit marine8 = new BionicUnit(BionicUnit.BionicUnitType.Marine, "마린8");
LoadUnit(medic);
LoadUnit(marine1);
LoadUnit(marine2);
LoadUnit(marine3);
LoadUnit(marine4);
LoadUnit(marine5);
LoadUnit(marine6);
LoadUnit(marine7);
LoadUnit(marine8);
UnLoad(medic);
UnLoadAll();
}
public void LoadUnit(BionicUnit unit)
{
if (units.Count >= CAPACITY)
{
Debug.Log("<color=red> 자리가 없어서 탑승에 실패 </color>");
}
else
{
units.Add(unit);
Debug.LogFormat("{0}이 탑승했습니다. ({1}/{2})", unit.type, units.Count, CAPACITY);
//Debug.LogFormat("{0}이 탑승했습니다. ({1}/{2})", unit.name, units.Count, CAPACITY);
}
}
public void UnLoad(BionicUnit unit)
{
this.units.Remove(unit);
Debug.LogFormat("{0}이 하차했습니다. ({1},{2})", unit.type, units.Count, CAPACITY);
}
public void UnLoadAll()
{
Debug.Log("모두 하차 시작");
foreach (BionicUnit unit in units.ToList())
//()이나 Start() 메서드에서 units 리스트를 수정하지 않도록 합니다. 리스트를 수정할 필요가 있다면 반복문 밖에서 수정
{
UnLoad(unit);
}
}
}
이런식으로 .ToList()를 쓰지 않으면 오류가 난다.
이유를 찾아보니 멤버변수에 선언한 "원본" 리스트를 변경시키기 때문에 나는 현상이라고 한다.