Game-Week1.1) Unity function

gyu·2023년 1월 18일
0

  • animation 만들기
    -asset(inspector)에서 create animation folder
    -right click -> create animation
    -by checking loop, make animation repeat actions
    -drag animation to the controller(ex) character file in scene)
    +controller -언제 어떤 동작을 실행할지 결정하는 파일

  • 캐릭터 움직이게 하기
    transform.position += new Vector3(0.05f, 0, 0);
    (캐릭터 포지션 x가 0.05 더한만큼 움직이기)

-transform : 캐릭터의 포지션 변경
+= vector3(x,y,z)만큼을 더하기
소수를 쓰고 싶을때 float(f)를 붙여주기

  • 마우스 클릭시 캐릭터 좌우반전해주기

        if(Input.GetMouseButtonDown(0))
        {
                scale *= -1f;
                direction *= -1f;

        }
        

0 -왼쪽마우스버튼
1 -오른쪽마우스버튼
-키보드 또는 마우스의 이벤트를 받아야 할때 Input class 사용
키보드 - getKeyDown()
마우스 - getMouseButtonDown(0||1)


  • 충돌기능 만들기

    -충돌의 기본조건
    1) 둘다 colider가 있어야 함
    2) 둘 중 하나는 rigidBody가 있어야 함
    colider에 부딫혔을 때 실행되는 함수( OnCollisionEnter2D)

    void OnCollisionEnter2D(Collision2D coll){
    if( coll.gameObject.tag == "ground"){
    //remove object
    Destroy(gameObject);
    }
    }

  • random function을 사용하기

    -x,y에 Random.Range() 사용하여 랜덤 위치를 지정해줌
    float x = Random.Range(-2.7f, 2.7f);
    float y = Random.Range(3f, 5f);
    transform.position = new Vector3(x, y, 0);

    -랜덤 사이즈, 색깔 지정하기
    size - transform.localScale
    color - GetComponent().color = new Color( 100/255f ...)
    - 255f로 나눠주는 게 핵심(소수로 표현하기 위해서)


    gameManager

  • 게임 전체를 다루는 오브젝트 (ex) 부스터, 광고관련, 점수,...)
    *sigleton (게임을 실행하면서 game manager는 무조건 1개여야함)
    어디서도 부를 수 있는 1개의 obj로 만들어주기
    -how?->

    public static GameManager l;
    
      private void Awake()
      {
          l = this;
      }

Prefabs

  • 만들어 놓은 코드를 계속 가져다 쓸 수 있게 해주는 기능
  • 한번 만들어놓으면 무한정 복제가 가능-how?->
    복제하는 법
    1) define gameObject var
    2) gameManager에서 gameObj drag- drop
    3) invokeRepeating("methodname", 0 , howManyTimes)
    4) create 'methodname' method to instantiate
{
  	//2)
    public GameObject rain;
    // Start is called before the first frame update
    void Start()
    {
  		//3) 0.5초마다 makeRain을 실행시켜라
        InvokeRepeating("makeRain", 0 ,  0.5f);
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    void makeRain()
    {
        Instantiate(rain);
    }
}

정보주는 부분( 점수, 시간..) -> UI로 표현

  • UI
    -canvas도화지 위에 그려지고 카메라 위치와는 관계없이 보여주는 obj
    -버튼, 텍스트, 순위 등을 보여줄때 사용
  • 시간 관련 기능들
  • 시간잴때 사용되는 함수(Time.deltaTime 걸린시간)
    limit(게임시간) -= Time.deltaTime(걸린시간)
  • 소수점자리 표현
    ex) 두번째자리까지 보여주기
    limit.ToString("N2");
    -시간 멈추게하기(빠르게, 느리게 가능)
    Time.timeScale
profile
#TechExplorer 🚀 Curious coder exploring the tech world, documenting my programming journey in a learning journal

0개의 댓글