반응형

Input 클래스 : 키보드 입력

 

Input.anyKeyDown // 아무입력을 최초로 받을 때 true
Input.anyKey // 아무입력을 받으면 true
Input.GetKeyDown // 키를 눌렀을때
Input.GetKey // 키를 누르고 있을때
Input.GetKeyUp // 키를 땠을 때
Input.GetMouseButtonUp(0) // 마우스 버튼 누름
Input.GetMouseButton(1) // 마우스 버튼 꾹 누름
Input.GetMouseButtonDown() // 마우스 버튼 땜
Input.GetButton("Jump") // input manager 상의 버튼을 사용가능
Input.GetAxis() // 수평, 수직 버튼 입력을 받으면 float, 꾹 누르면 가중치
Input.GetAxisRaw() // 1, -1 만 표시 좌, 우를 알 수 있음
// 0 : 왼쪽, 1 : 오른쪽

//Program Settings => Input manager 에서 버튼 세팅 가능

transform : 오브젝트는 변수 transform 을 항상 가지고 있음

transform.Translate(Vector 3) // 벡터 값을 현재 위치에 더하는 함수 

Vector 3 = new Vector 3(0,0,0) // 벡터 선언

Vector3 : 3차원 벡터 

Vector3.MoveTowards(현재위치, 목표위치, 속도 ) // 등속이동
Vector3.SmoothDamp(현재위치, 목표위치, ref, 속도) //부드러운 감속이동 속도에 반비례하여 이동
Vector3.Lerp() //SmoothDamp 보다 감속시간이 김 속도에 비례하여 이동
Vectro3.Slerp() //구면 선형보간, 호를 그리며 이동

Time.deltaTime

Translate : 벡터에 곱하기
translate.Translate(Vec * Time.deltaTime);

Vector : 시간 매개변수에 곱하기
Vector3.Lerp(Vec1, Vec2, T * Time.deltaTime);

//이전 프레임의 완료까지 걸린 시간 
// 프레임이 적으면 크고, 프레임이 많으면 적음

Rigidbody : 중력

  • Is Kinematic : 외부 물리 효과를 무시 (움직이는 함정)
  • FixedUpdate 에 작성해야한다

Collider : 물리 효과를 받기 위한 컴포넌트

material : 오브젝트 디자인

Physics Material : 탄성과 마찰을 다루는 물리적 재질

  • Collider 내부안의 인자
  • Bounciness : 탄성력
  • Bounciness Combine : 다음 탄성을 계산하는 방식
  • Friction : 마찰력
    • Dynamic : 동적 마찰력
    • Static : 정지 마찰력
GetComponent<T>() //T 타입의 컴포넌트르 가져옴

Rigidbody rigid = new Rigidbody();
Rigid.velocity // 속도를 바꿈
Rigid.AddForce //vec 방향과 크기로 힘을 줌 , 힘이 계속 더해짐
Rigid.AddTorque(Vec) // vec 방향을 축으로 회전력생김

ForceMode : 힘을 주는 방식
ForceMode.Impulse // 무게에 따라 달라짐

물리적 충돌(Collision), 콜라이더(Collider) 충돌

//실제 물리적 충돌
OnCollisionEnter(collision) // 물리적 충돌이 시작할 때 호출
OnCollisionExit(collision) //물리적 충돌이 끝났을 때 호출
OnCollisionStay(collision) // 충돌이 쭉 이어갈 때

//콜라이더 충돌
OnTriggerEnter(collider),Exit,Stay//물리적 충돌 x
반응형

'Unity > BE1' 카테고리의 다른 글

B1 : Life Cycle  (0) 2021.07.12
반응형

 

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

public class LifeCycle : MonoBehaviour
{
    // Start is called before the first frame update
    void Awake()// 게임 오브젝트 생성시, 최초실행
    {
        Debug.Log("Player Data has been ready.");
    }

    void Start()//업데이트 시작전 최초 실행
    {
        Debug.Log("사냥 장비를 챙겼습니다.");
    }

    //////////////초기화 영역//////////////////
    void OnEnable()
    {
        Debug.Log("플레이어 로그인");
    }

    /////////////활성화 영역///////////////
    void FixedUpdate()//물리 연산 업데이트 , 물리연산 확인 전 실행, cpu 많이 사용
    {
        Debug.Log("Move");
    }

    
    /////////////물리 영역////////////////////
    ///
    void Update()// 게임 로직 변환, 주기적, 환경에 따라 실행주기가 떨어진다, 물리연산보다 빠름
    {
        Debug.Log("사냥");
    }

    void LateUpdate() // 모든 업데이트 후 마지막 업데이트, 
    {
        Debug.Log("EXP");
    }


    ////////////게임로직////////////////////
    ///
    void OnDisable()
    {
        Debug.Log("플레이어 로그아웃");
    }

    
    /// ////////비활성화영역//////////////
    
    void OnDestroy() //게임오브젝트를 삭제할 때, 
    {
        Debug.Log("Player has been eliminated");
    }

    //////////////해체///////////////
    ///
}
반응형

'Unity > BE1' 카테고리의 다른 글

B2 : Input, Vector3, Other Components  (0) 2021.07.13
반응형

https://www.youtube.com/watch?v=4egDnkVkjv8 

 

Wall.cs

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

public class Wall : MonoBehaviour
{

    public Sprite dmgSprite;  // 플레이어가 벽을 한대 때렸을 때 보여줄 스프라이트  
    public int hp = 4; // 벽의 체력

    private SpriteRenderer spriteRenderer;

    // Start is called before the first frame update
    void Awake()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
    }

    public void DamageWall (int loss)
    {
        spriteRenderer.sprite = dmgSprite; // 플레이어가 성공적으로 벽을 공격했을떄 시각적인 변화를 줌
        hp -= loss;
        if (hp <= 0)
            gameObject.SetActive(false); //hp 0 이라면 게임오브젝트 비활성화
    }
}

 

Player.cs

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

public class Player : MovingObject
{
    public int wallDamage = 1; //플레이어가 가하는 공격력
    public int pointsPerFood = 10;
    public int pointsPerSoda = 20;
    public float restartLevelDelay = 1f;

    private Animator animator; //에니메이터 레퍼런스를 가져오기 위한 변수
    private int food;

    // Start is called before the first frame update
    protected override void Start() //MovingObject 에 있는 start 와 다르게 구현하기 위해 오버라이딩
    {
        animator = GetComponent<Animator>();
        food = GameManager1.instance.playerFoodPoints; // 해당레벨동안 음식점수를 관리할 수 있음, 레벨이 바뀔 때 마다 게임메니저에 저장 

        base.Start(); // 부모 클래스의 스타트 호출
    }

    private void OnDisable()
    {
        GameManager1.instance.playerFoodPoints = food;  //food 를 게임메니저에 저장하는 과정  
    }

    void Update()
    {
        if (!GameManager1.instance.playersTurn) return; //플레이어의 턴이 아니라면 종료

        int horizontal = 0;
        int vertical = 0;

        horizontal = (int)Input.GetAxisRaw("Horizontal");
        vertical = (int)Input.GetAxisRaw("Vertical");

        if (horizontal != 0)
            vertical = 0;//플레이어가 대각선으로 움직이는 것을 막음

        if (horizontal != 0 || vertical != 0)// 움직이려고 하면 
            AttemptMove<Wall>(horizontal, vertical);//벽에 부딪힐 때(움직이려는 방향으로)
    }

    protected override void AttemptMove <T> (int xDir, int yDir)
    {
        food--;//움직이면 푸드 -1

        base.AttemptMove<T>(xDir, yDir);

        RaycastHit2D hit; // 충돌여부 변수 

        CheckIfGameOver();

        GameManager1.instance.playersTurn = false;
    }

    private void OnTriggerEnter2D (Collider2D other)//출구, 소다 , 음식의 태그를 트리거로 설정했으므로 태그를 체크
    {
        if (other.tag == "Exit")
        {
            Invoke("Restart", restartLevelDelay);//restartLevelDelay 만큼 정지 후 함수 호출 
            enabled = false;
        }
        else if (other.tag == "Food")
        {
            food += pointsPerFood;
            other.gameObject.SetActive(false);//푸드, 소다 오브젝트를 비활성화
        }
        else if (other.tag == "Soda")
        {
            food += pointsPerSoda;
            other.gameObject.SetActive(false);
        }
    }

    protected override void OnCantMove <T> (T component)//wall 에 의해 block 되는 경우를 표현 
    {
        Wall hitWall = component as Wall;
        hitWall.DamageWall(wallDamage);//플레이어가 벽에 얼마나 데미지를 가하는지
        animator.SetTrigger("playerChop");//애니메이션 트리거
    }

    private void Restart()//Exit 오브젝트와 충돌시 발생하는 함수
    {
        SceneManager.GetActiveScene(); // 레벨을 다시 불러옴 , 이 게임에는 하나 밖에 없는 main 신을 불러옴, 많은 게임들이 이 함수를 이용해 다른 신을 불러온다
        //SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }

    public void LoseFood(int loss)
    {
        animator.SetTrigger("playerHit");
        food -= loss;
        CheckIfGameOver();
    }

    private void CheckIfGameOver()
    {
        if (food <= 0)
            GameManager1.instance.GameOver();
    }
}

 

새로 알게 된 내용

  •  SceneManager.GetActiveScene(); 
    • 많은 게임들이 이 함수를 이용해 신에서 신으로 넘어가는 작업을 한다
  • 1. Input.GetAxis(string name)
    • -1, 0, 1 세 가지 값 중 하나가 반환된다. 키보드 값을 눌렀을 때 즉시 반응해야한다면 GetAxisRaw를 사용하면 된다.  
  • 2. Input.GetAxisRaw(string name)
    • -1.0f 부터 1.0f 까지의 범위의 값을 반환한다. 즉, 부드러운 이동이 필요한 경우에 사용된다.
    • 키보드 입력을 위해 getAxisRaw 를 사용한다
  • RayCastHit
    • 원점에서 쏜 레이를 통해 충돌 여부를 파악
반응형

'Unity > 로그라이크 따라해보기' 카테고리의 다른 글

로그라이크 따라하기 6  (0) 2021.07.07
반응형

https://www.youtube.com/watch?v=LrpaW6VglVU 

 

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


//일반형(generic) 을 사용하는 이유 플레이어, 적, 벽 과의 상호작용에서 서로서로 hitComponent 의 종류를 알 수 없기 때문에 당장 OnCantMove 함수에 입력을 받고 각각의 상속한 클래스들의 구현에 따라 동작하게 할 수 있기 때문이다.
public abstract class MovingObject : MonoBehaviour
{
    public float moveTime = 0.1f;
    public LayerMask blockingLayer; //충돌여부 변수


    private BoxCollider2D boxCollider;
    private Rigidbody2D rb2D; //객체의 움직임 레퍼런스를 담을 변수
    private float inverseMoveTime; //움직임을 효율적이게 계산하기 위한 변수


    // Start is called before the first frame update
    protected virtual void Start()//자식 클래스가 덮어써서 재정의 하기 위해 start 를 다시 정의할 수도있어서
    {
        boxCollider = GetComponent<BoxCollider2D>();
        rb2D = GetComponent<Rigidbody2D>();
        inverseMoveTime = 1f / moveTime; //나누기 대신에 효율적인 곱하기를 사용하기 위해 움직임의 역수치를 저장
    }

    protected bool Move (int xDir, int yDir, out RaycastHit2D hit)//bool 리턴값, hit 리턴값 두개의 리턴값을 가짐
    {
        Vector2 start = transform.position; //현재위치를 받기위한 변수
        // position 은 기존 3차원 이지만 Vector 2에 저장함으로 z 값은 날라가게됨 
        Vector2 end = start + new Vector2(xDir, yDir);

        boxCollider.enabled = false;//자기자신과 부딛히지 않기 위해 boxCollider 해제
        hit = Physics2D.Linecast(start, end, blockingLayer); // 시작지점과 끝지점 까지의 라인을 가져오고 blockinglayer 와 충돌검사
        boxCollider.enabled = true; 

        if(hit.transform == null)//부딪힌게 없다면
        {
            StartCoroutine(SmoothMovement(end));//end 방향으로 이동
            return true;
        }
        return false;
    }


    protected IEnumerator SmoothMovement (Vector3 end)//객체를 움직이게 할 함수 end에 어디로 움직일지 저장
    {
        float sqrRemainingDistance = (transform.position - end).sqrMagnitude; // 현재위치 - end 벡터에 sqrMagnitude 로 거리계산 Magnitude : 벡터길이 , sqrMagnitude : 벡터길이 제곱 

        while (sqrRemainingDistance > float.Epsilon)
        {
            Vector3 newPosition = Vector3.MoveTowards(rb2D.position, end, inverseMoveTime * Time.deltaTime); // epsilon (0에 가까운 아주작은 수 ) newposition 을 계산하여 end 값과 가장가까운 위치로 이동시키기 위한 변수
            //(현재위치(rigidpositon), 이동할위치, 시간(이 변수의 단위만틈 목적지로 가까워짐)) 
            rb2D.MovePosition(newPosition); // 새로운 위치로 이동
            sqrRemainingDistance = (transform.position - end).sqrMagnitude; // 움직인 후 남은 거리 계산 
            yield return null; //루프를 갱신하기 전에 다음 프레임을 기다림
        }
    }

    protected virtual void AttemptMove <T> (int xDir, int yDir)
        where T : Component
    {
        RaycastHit2D hit;
        bool canMove = Move(xDir, yDir, out hit);//이동하는데 성공하면 canMove = true else flase    

        if (hit.transform == null)//다른것과 부딪히지 않았다면 코드 종료
            return;

        T hitComponent = hit.transform.GetComponent<T>(); //충동할 레퍼런스를 T 타입 컴포넌트에 할당 

        if (!canMove && hitComponent != null)//움직임이 막힘 -> 충돌
            OnCantMove(hitComponent);
    }

    protected abstract void OnCantMove<T>(T component)
        where T : Component;// 자식 클래스에서 오버라이드 위해 남겨둠 
}

 

일반형(Generic) 타입을 사용하는 이유 

 

플레이어, 적, 벽 과의 상호작용에서 서로서로 hitComponent 의 종류를 알 수 없기 때문에 당장 OnCantMove 함수에 입력을 받고 각각의 상속한 클래스들의 구현에 따라 동작하게 할 수 있기 때문이다.

반응형

'Unity > 로그라이크 따라해보기' 카테고리의 다른 글

로그라이크 따라하기 7,8,9  (0) 2021.07.09

+ Recent posts