본문 바로가기
게임개발/게임플랫폼 응용 프로그래밍

시즈탱크 1단계 - 에러 수정전

by pudding81 2024. 2. 29.

 

 

클릭 1 멈춤
클릭 2  멈춤
클릭 3 멈추지않음
클릭 4 클릭한 곳으로 돌아옴

클릭 5 멈추지않음
클릭 6 돌아옴
클릭7 멈추지않음

 

 

 

 

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class Tank : MonoBehaviour
{
    public enum TankType
    {
        Tank, Siege
    }

    public TankType state;

    public GameObject tank;
    public GameObject siege;


    private Coroutine coroutine;
    public float speed=2f;

    Vector3 dir;

    private void Awake()
    {
        Debug.LogFormat("{0}(이)가 생성되었습니다.", this);
    }


    void Start()
    {
        tank = transform.GetChild(0).gameObject;
        siege = transform.GetChild(1).gameObject;


    }


    void Update()
    {
        //화면을 클릭하면 Ray를 만든다 
        if (Input.GetMouseButtonDown(1)) //마우스 우클릭할때 
        {
            Vector3 mousePosScreen = Input.mousePosition;
            Vector2 mousePosWorld = Camera.main.ScreenToWorldPoint(mousePosScreen);

            RaycastHit2D hit = Physics2D.Raycast(mousePosWorld, Vector2.zero);



            //코루틴 

            this.dir = this.transform.position;
              
            this.dir= hit.point;

            Debug.LogFormat("마우스 클릭위치 {0}",dir);    //목표 위치 



            if (this.coroutine != null)
            {
                StopCoroutine(this.coroutine);

                this.transform.position = this.dir;

            }

            this.coroutine = StartCoroutine(this.Move(dir));

            
        }
       


        if (Input.GetKeyDown(KeyCode.O))
        {
           
            ChangeStateToggle();
            
            switch (this.state)
            {
                case TankType.Tank:                   
                    On();
                    break;
                case TankType.Siege:
                    Off();
                    break;
            }


        }

       


    }


     IEnumerator Move(Vector3 dir)
    {
      
        //매 프레임마다 앞으로 이동 
        
        while (true)
        {
            
         
            this.transform.Translate(dir * speed * Time.deltaTime);

            // 이동하고자하는 좌표 값과 현재 내 위치의 차이를 구한다.
            float distance = (dir - this.transform.position).magnitude;

            Debug.LogFormat("distance: {0}", distance);


            if (distance <= 0.1f)
            {              
                break;
            }

            yield return null;

        }

        Debug.Log("이동완료");
    }

    private void ChangeStateToggle()
    {
        if (this.state == TankType.Tank)
        { this.state = TankType.Siege; }
        else
        { this.state = TankType.Tank; }

    }

    private void On()
    {
        tank.SetActive(false);
        siege.SetActive(true);
    }

    private void Off()
    {
        tank.SetActive(true);
        siege.SetActive(false);
    }


    private void SetState(TankType state)
    {
        if (TankType.Siege ==state)
        {
            this.transform.position=Vector3.zero; 
            
        }



    }

}