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;
    }
}

 

 

 

 

 

[SerializeField] private Sprite image1;

[SerializeField] private Image image1;

두개의 차이

 

 

'산대특 > 게임 클라이언트 프로그래밍' 카테고리의 다른 글

Quaternion  (1) 2024.03.04
Dodge game  (0) 2024.03.04
코루틴 연습  (0) 2024.03.03
StarCraft - DropShip Logic  (0) 2024.02.28
Find the nearest object  (0) 2024.02.27

+ Recent posts