2 분 소요

1.기본적인 움직임

1-1 방향키로 움직이기

    void Move() 
    { 
        if (Input.GetKey(KeyCode.UpArrow)) 
        { 
            this.gameObject.transform.Translate(0, 0, _moveSpeed * Time.deltaTime);          // 1초에 5만큼 움직인다 
        } 
        if (Input.GetKey(KeyCode.DownArrow)) 
        { 
            this.gameObject.transform.Translate(0, 0, -_moveSpeed * Time.deltaTime); 
        } 
        if (Input.GetKey(KeyCode.LeftArrow)) 
        { 
            this.gameObject.transform.Translate(-_moveSpeed * Time.deltaTime, 0, 0); 
        } 
        if (Input.GetKey(KeyCode.RightArrow)) 
        { 
            this.gameObject.transform.Translate(_moveSpeed * Time.deltaTime, 0, 0); 
        } 
    } 

1-2 점프하기

  • 장애물2개가있고 → cube 2개 생성
  • 그것을점프해서 넘겨라 → 떨어지는 중력, 아래에서 위로 미는 힘
  • 부딪히면 콘솔창에 다침이라고 띄워라!
  • 넘어가서 마지막에는 Goal 표시가 있는데
  • 그 Goal에 도착하면 콘솔창에 게임 종료라고 띄워라.

  • 점프하는 방법
  • 스페이스바를 누르면 점프하게 해주세요
  • 이 스크립트가 들어가있는 게임오브젝트가 아래에서 위방향으로 1회성의 힘을 받는다.
    void Jump()         //점프를 하는 함수 
    { 
      if (Input.GetKeyDown(KeyCode.Space) && _isGround == true)                    
       { 
           this.gameObject.GetComponent<Rigidbody>().AddForce(Vector3.up * _addforce);                 
          _isGround = false; 
        } 
    } 
    private void OnCollisionEnter(Collision collision) 
    { 
        _isGround = true; 
    } 

    private void OnTriggerEnter(Collider other) 
    { 
        if (other.tag == "Obstacle") 
        { 
            print("다침!"); 
        } 
        if (other.tag == "Goal")) 
        { 
            print("종료"); 
        } 
    } 

캡슐이 넘어지지 않게 하기(회전 하지못하게)

image

  • Constraints
  • Freeze Rotation : x z 체크

2. 카메라 이동

image

  • 캡슐 밑에(자식으로) 메인카메라와 1인칭, 3인칭용 빈 오브젝트(위치,각도 설정)를 생성한다.

2-1 1인칭 : 캡슐의 머리 부분

image

2-2 3인칭 : 캡슐 에서 살짝 뒤, 살짝 위, 각도 밑으로

image

  • V키를 누를대마다 시점 변환하게 해주세요
  • V키를 누르면, 메인카메라의 위치와 각도를 cameraTr이라는 배열의 0번째(1인칭) 또는 1번째(3인칭) 값의 위치 및 각도와 같게 하라
    public Transform[] cameras; 
    int cameraNum;
        void Update() 
    { 
        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; 
            } 
            if (cameraNum == 1) 
            { 
                Camera.main.transform.position = cameraTr[0].transform.position; 
                Camera.main.transform.rotation = cameraTr[0].transform.rotation; 
                cameraNum = 0; 
            } 
        } 
    }

최종 값

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

public class Player : MonoBehaviour 
{ 
    // Start is called before the first frame update 
    bool _isGround; 
    bool _isCamera; 
    public int _addforce; 
    public int _moveSpeed; 
    public Transform[] cameras; 

    void Awake() 
    { 
    } 
    void Start() 
    { 
    } 

    // Update is called once per frame 
    void Update() 
    { 
        Move(); 
        Jump(); 
        if (Input.GetKeyDown(KeyCode.V)) 
        { 
            if(_isCamera == false) 
            { 
                Camera.main.transform.position = cameras[0].transform.position;  
                Camera.main.transform.rotation= cameras[0].transform.rotation;  
                _isCamera= true; 
            }            
            else 
            { 
                Camera.main.transform.position = cameras[1].transform.position;  
                Camera.main.transform.rotation= cameras[1].transform.rotation;  
                _isCamera= false; 
            }    
        } 
    } 

    void Move() 
    { 
        if (Input.GetKey(KeyCode.UpArrow)) 
        { 
            this.gameObject.transform.Translate(0, 0, _moveSpeed * Time.deltaTime);          // 1초에 5만큼 움직인다 
        } 
        if (Input.GetKey(KeyCode.DownArrow)) 
        { 
            this.gameObject.transform.Translate(0, 0, -_moveSpeed * Time.deltaTime); 
        } 
        if (Input.GetKey(KeyCode.LeftArrow)) 
        { 
            this.gameObject.transform.Translate(-_moveSpeed * Time.deltaTime, 0, 0); 
        } 
        if (Input.GetKey(KeyCode.RightArrow)) 
        { 
            this.gameObject.transform.Translate(_moveSpeed * Time.deltaTime, 0, 0); 
        } 
    } 
    void Jump() 
    { 
        if (Input.GetKeyDown(KeyCode.Space) && _isGround == true) 
        { 
            this.gameObject.GetComponent<Rigidbody>().AddForce(Vector3.up * _addforce); 
            _isGround= false; 
        }     
    } 
    private void OnCollisionEnter(Collision collision) 
    { 
        _isGround= true; 
    } 
    private void OnTriggerEnter(Collider other) 
    { 
        if(other.tag == "Obstacle") 
        { 
            print("부딪힘!"); 
        } 
        if(other.tag == "Player") 
        { 
            print("종료"); 
        } 
    } 
} 

댓글남기기