2 분 소요

기본 순서

  1. Unity Client에서 제작
    • Material, metalic, soomth지정, prefabs로 지정
  2. 변수 지정
  3. 스크립트 제작

실습1. 컨트롤키 누를때마다 총 쏘세요

  1. 총알이 필요 1-1 총알이라는 GameObject를 prefab으로 설정
  2. 총알을 생성하는 코드 제작 2-1 Instantiate

  3. 총알을 발사하는 위치를 지정
    • insBullet.transform.position = firePos.position;
    • 4 . 총알에 힘을 가함
    • 총알에 rigidbody가있음
    • Addforce(힘,방향)
    • 방향? 총알의 앞쪽
    • 힘? 1000
    void Shoot()
    {
        if (Input.GetKeyDown(KeyCode.LeftControl))
        {
            GameObject insBullet = Instantiate(bulletPrefab);
            insBullet.transform.position = firePos.position;
            insBullet.GetComponent<Rigidbody>().AddForce(transform.forward * 1000);

            if (insBullet.GetComponent<Rigidbody>() == null)
            {
                insBullet.AddComponent<Rigidbody>();        //컴퍼넌트 만들기
            }
        }
    }

실습2. 총알이 만들어진 후에 5초 지나면 사라지게 해주세요

  • Instantiate(오브젝트);
  • Destroy(오브젝트);
  • 만들어지자마자 시간을 세는데(Time.deltaTime)
  • 그 시간이 5초보다 커지면 그러면 Destroy한다.
void Update()
    {
        interval += Time.deltaTime;         
        if(interval > 5)
        {
            MyDestroy();
        }
    }

    void MyDestroy()
    {
        Destroy(this.gameObject);
        
    }

그외…

  • 원사이디드 : 한면은 뚫리고 한면은 닫힘

image

  • 더블사이디드: 양면이 다 닫힘

  • Plane은 y축은 커지지 않음
  • Quad는 z축으로 커지지 않음(가볍지만 잘 안씀/영화관)
  • Terriain은 plane에 비해 무거움, 맵구현 가능, 메타버스에는X

###

using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;

public class Player : MonoBehaviour
{
    public GameObject obj;
    public GameObject insObj;

    int _jumpNum;
    public GameObject bulletPrefab;
    public int _moveSpeed;
    public int _rotSpeed;
    public int _addforce = 100;
    public GameObject pefabs;
    public Transform firePos;
    int cameraNum;
    public Transform[] cameraTr;


    void Start()
    {
    }

    void Update()
    {
        Move();
        Jump();
        See();
        RayPoint();
        Shoot();
    }

    void Shoot()
    {
        if (Input.GetKeyDown(KeyCode.LeftControl))
        {
            GameObject insBullet = Instantiate(bulletPrefab);
            insBullet.transform.position = firePos.position;
            insBullet.GetComponent<Rigidbody>().AddForce(transform.forward * 1000);

            if (insBullet.GetComponent<Rigidbody>() == null)
            {
                insBullet.AddComponent<Rigidbody>();        //컴퍼넌트 만들기
            }
        }
    }


    //이동하는 함수
    void Move()
        {
            if (Input.GetKey(KeyCode.UpArrow))

            {
                this.gameObject.transform.Translate(0, 0, _moveSpeed * Time.deltaTime);
            }

            if (Input.GetKey(KeyCode.DownArrow))
            {
                this.gameObject.transform.Translate(0, 0, -_moveSpeed * Time.deltaTime);
            }

            if (Input.GetKey(KeyCode.LeftArrow))
            {
                this.gameObject.transform.Rotate(0, -_rotSpeed* Time.deltaTime, 0);
            }

            if (Input.GetKey(KeyCode.RightArrow))
            {
                this.gameObject.transform.Rotate(0, _rotSpeed* Time.deltaTime, 0);
            }
        }

        //점프하는 함수
        void Jump()
        {
            if (Input.GetKeyDown(KeyCode.Space) && _jumpNum == 0)
            {
                this.gameObject.GetComponent<Rigidbody>().AddForce(Vector3.up * _addforce);
                _jumpNum++;
            }
            else if (Input.GetKeyDown(KeyCode.Space) && _jumpNum == 1)
            {
                this.gameObject.GetComponent<Rigidbody>().AddForce(Vector3.up * _addforce);
                _jumpNum++;
            }
            else if (Input.GetKeyDown(KeyCode.Space) && _jumpNum == 2)
                return;
        }

        void RayPoint()
        {
            if (insObj == null)
            {
                if (Input.GetMouseButtonDown(0))
                {
                    Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
                    RaycastHit rHit;
                    if (Physics.Raycast(r, out rHit, Mathf.Infinity))
                    {
                        //GameObject insObj = Instantiate(obj.gameObject, rHit.point, Quaternion.Euler(0,0,0));
                        insObj = Instantiate(obj.gameObject);
                        Destroy(insObj, 5);
                    }
                }
            }
            else if(insObj != null)
            {

                Ray raypointer = new Ray();
                raypointer = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hitPoint;

                if (Physics.Raycast(raypointer, out hitPoint, Mathf.Infinity))
                {
                insObj.transform.position = new Vector3(hitPoint.point.x, hitPoint.point.y + 0.5f, hitPoint.point.z);

                }
                if (Input.GetMouseButtonDown(0))
                {
                    insObj = null;
                }

            }

        }

        //카메라 시점변경
        void See()
        {
            if (Input.GetKeyDown(KeyCode.V))
            {
                if (cameraNum == 0)
                {
                    Camera.main.transform.position = cameraTr[1].transform.position;
                    Camera.main.transform.rotation = cameraTr[1].transform.rotation;
                    cameraNum = 1;
                }
                else if (cameraNum == 1)
                {
                    Camera.main.transform.position = cameraTr[0].transform.position;
                    Camera.main.transform.rotation = cameraTr[0].transform.rotation;
                    cameraNum = 0;
                }
            }
        }

        void OnCollisionEnter(Collision collision)
        {
            _jumpNum = 0;
        }


        void OnTriggerEnter(Collider other)
        {

            if (other.tag == "Obstacle")
            {
                print("부딪힘!!");
            }
            if (other.tag == "Goal")
            {
                print("종료");
            }

        }
    }

댓글남기기