아카이브

8/10 몬스터 생성/삭제 복습 본문

카테고리 없음

8/10 몬스터 생성/삭제 복습

timbercat 2023. 8. 11. 05:07

히어로 좌클릭 생성 우클릭 삭제

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

public class Test_CreateItem : MonoBehaviour
{
    [SerializeField]
    private GameObject prefab;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //좌클릭하면 히어로 생성되게 한다
        if(Input.GetMouseButtonDown(0))
        {
            //ray 만들기
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            //출력
            Debug.DrawRay(ray.origin, ray.direction * 1000f, Color.blue, 5f);
            RaycastHit hit;
            //충돌검사
            if(Physics.Raycast(ray, out hit, 1000f))
            {
                //충돌 지점의 월드 좌표 
                Debug.Log(hit.point);
                //충돌한 콜라이더의 게임오브젝트의 태그가 Ground라면 
                if (hit.collider.gameObject.tag == "Ground")
                {
                    //프리팹으로 게임오브젝트 생성
                    GameObject go = Instantiate(this.prefab);
                    //위치설정
                    go.transform.position = hit.point;
                }
            }
        }
        //우클릭하면 삭제
        else if(Input.GetMouseButtonDown(1)) 
        {
            //레이 만들기
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            //출력
            Debug.DrawRay(ray.origin, ray.direction * 1000f, Color.yellow, 5f);
            RaycastHit hit;
            //충돌검사
            if (Physics.Raycast(ray, out hit, 100f))
            {
                //충돌한 콜라이더 태그가 Hero면
                if(hit.collider.gameObject.tag == "Hero")
                {
                    //제거
                    Destroy(hit.collider.gameObject);
                }
            }
        }
    }
}

 

좌클릭 hp포션 생성 우클릭 마나포션 생성

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

public class Test_CreateItem : MonoBehaviour
{
    [SerializeField]
    private GameObject hpPrefab;
    [SerializeField]
    private GameObject manaPrefab;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //좌클릭하면 체력물약 생성
        if(Input.GetMouseButtonDown(0))
        {
            //ray 만들기
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            //출력
            Debug.DrawRay(ray.origin, ray.direction * 1000f, Color.blue, 5f);
            RaycastHit hit;
            //충돌검사
            if(Physics.Raycast(ray, out hit, 1000f))
            {
                //충돌 지점의 월드 좌표 
                Debug.Log(hit.point);
                //충돌한 콜라이더의 게임오브젝트의 태그가 Ground라면 
                if (hit.collider.gameObject.tag == "Ground")
                {
                    //hp포션 생성
                    GameObject go = Instantiate(this.hpPrefab);
                    //위치설정
                    go.transform.position = hit.point;
                }
            }
        }
        //우클릭하면 마나물약 생성
        else if(Input.GetMouseButtonDown(1)) 
        {
            //레이 만들기
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            //출력
            Debug.DrawRay(ray.origin, ray.direction * 1000f, Color.yellow, 5f);
            RaycastHit hit;
            //충돌검사
            if (Physics.Raycast(ray, out hit, 100f))
            {
                //충돌한 콜라이더 태그가 Ground면
                if(hit.collider.gameObject.tag == "Ground")
                {
                    // 마나포션 생성
                    GameObject go = Instantiate(this.manaPrefab);
                    //위치설정
                    go.transform.position = hit.point;
                }
            }
        }
    }
}