[24.02.16]Unity - 개인(MiniGame)

손지·2024년 2월 16일
0

Unity

목록 보기
10/44

배웠던걸 가지고 캐릭터가 마우스로 클릭으로 이동을 하고

코인먹으면 코인점수가 오르는 게임을 대충 만들어보자.

우선 전 프로젝트에서 코인모양의 오브젝트를 추가해준다(물론 머테리얼도) 그리고 태그도 코인으로 넣어줍니다.

그리고 공에 트리거를 온 해두고

Area 라는 빈 프로젝트를 만들어

랜덤으로 공이 나올곳을 지정해줍니다.


Area.cs

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

public class Area : MonoBehaviour
{
    [SerializeField] private GameObject[] coins = { null };
    [SerializeField] private GameObject[] prefabs;
    private BoxCollider area;
    private GameObject areaobject = null;
    public int count = 100;

    private List<GameObject> gameObjects = new List<GameObject>();


    private void Awake()
    {
        area = GetComponent<BoxCollider>();
    }
    void Start()
    {



        for (int i = 0; i < count; ++i)//count 수 만큼 생성한다
        {
            Spawn();//생성 + 스폰위치를 포함하는 함수
        }

        area.enabled = false;
    }

    private Vector3 GetRandomPosition()
    {
        Vector3 basePosition = transform.position;
        Vector3 size = area.size;

        float posX = basePosition.x + Random.Range(-size.x / 2f, size.x / 2f);
        float posY = basePosition.y + Random.Range(-size.y / 2f, size.y / 2f);
        float posZ = basePosition.z + Random.Range(-size.z / 2f, size.z / 2f);

        Vector3 spawnPos = new Vector3(posX, posY, posZ);

        return spawnPos;
    }

    private void Spawn()
    {
        int selection = Random.Range(0, prefabs.Length);

        GameObject selectedPrefab = prefabs[selection];

        Vector3 spawnPos = GetRandomPosition();//랜덤위치함수

        GameObject instance = Instantiate(selectedPrefab, spawnPos, Quaternion.identity);
        gameObjects.Add(instance);
    }
}

coin.cs

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

public class Coin : MonoBehaviour
{
    private static int coinscore = 0;
    private void Awake()
    {
        coinscore = 0;
    }
    private void OnTriggerEnter(Collider other)
    {
            Destroy(this.gameObject);
            coinscore = coinscore + 1;
            Debug.Log(coinscore);
    }
  
}

pickingmove.cs

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

public class PickingMove : MonoBehaviour
{
    private Transform pointTr = null;
    [SerializeField] private float moveSpeed = 10f;
    [SerializeField] private float rotateSpeed = 10.0f;       // 회전 속도

    private const float stopDistance = 0.1f;
    

    private Vector3 pointPos = Vector3.zero;
    private bool isMoving = false;
    

    private void Awake()
    {
        pointTr = GameObject.FindGameObjectWithTag("PickingPoint").transform;
    }

    

    private void Update()
    {
        
        if (Input.GetMouseButtonDown(0))
        {
                isMoving = PickingProcess(ref pointPos);
        }

        if(isMoving)
        {
            SetPointPosition(pointPos);
            MovingToPoint(pointPos);
        }
    }
    private bool PickingProcess(ref Vector3 _point)
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if(Physics.Raycast(ray, out hit))
        {
            _point = hit.point;
            return true;
        }

        return false;
    }

    private void MovingToPoint(Vector3 _point)
    {
        _point.y = transform.position.y;
        Vector3 dir = _point - transform.position;

        dir.Normalize();

        

        transform.position = transform.position + (dir * moveSpeed * Time.deltaTime);
        /*Vector3 dist = transform.position - _point;
        dist.magnitude;*/
        //transform.LookAt(pointTr);
        if(Vector3.Distance(transform.position,  _point) < stopDistance)
        {
            isMoving = false;

        }
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(dir), Time.deltaTime * rotateSpeed);
    }

    private void SetPointPosition(Vector3 _pos)
    {
        pointTr.position = _pos;
    }
}
profile
게임 개발자가 될사람

0개의 댓글

관련 채용 정보