백지부터 시작하는 이세계 코딩 생활

Update(), Input.GetKey() 본문

백지부터 시작하는 이세계 유니티 생활 since 2020

Update(), Input.GetKey()

조아덕 2020. 11. 16. 23:28
Update () 

프로젝트 시작 후 주기를 가지고 연속적으로 호출되는 함수

Update : => Input.GetKey()에 주로 사용함.
스크립트가 enabled 상태일때, 매 프레임마다 호출됩니다 ( 프레임 기반으로 호출 된다. )

FixedUpdate : => RigidBody (물리엔진 작용)에 주로 사용됨.
Fixed Timestep에 설정된 값에 따라 일정한 간격으로 호출된다.

LastUpdate : => Camera View 처리에 주로 사용됨.
모든 Update 함수가 실행된 후 호출됨.

 

 

Update, FixedUpdate 함수에는 Input 조작값들만 사용하는 것을 권장한다.

연속호출이 필요한 부분InvokeRepeat이나 Coroutine을 통해 처리가능 하다. 

 

 


 

Input.GetKey()

키보드에서 입력받은 키를 기준으로 이벤트를 생성.

Input.GetKeyDown()

키보드가 눌렸을 때 이벤트를 생성.

Input.GetKeyUp()

키보드가 눌렸다 떨어질 때 이벤트를 생성.


 

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

public class PlayerController : MonoBehaviour
{
    private Rigidbody rigid;
    private float forwardForce = 1500.0f;
    private float sidewayForce = 1500.0f;
    // Start is called before the first frame update
    void Start()
    {
        rigid = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if (Input.GetKey("w"))
            rigid.AddForce(0, 0, forwardForce * Time.deltaTime);

        if (Input.GetKey("s"))
            rigid.AddForce(0, 0, -forwardForce * Time.deltaTime);

        if (Input.GetKey("a"))
            rigid.AddForce(-sidewayForce * Time.deltaTime, 0, 0);

        if (Input.GetKey("d"))
            rigid.AddForce(sidewayForce * Time.deltaTime, 0, 0);

        if (rigid.position.y < -1f)
            FindObjectOfType<GameManager_Youtube>().EndGame();        
    }
}

 


Ref.

developug.blogspot.com/2014/09/update-fixedupdate-lateupdate.html

 

Update() , FixedUpdate() , LateUpdate() 의 차이점

유니티에서 제공하는 Update 함수로 Update, FixedUpdate, LateUpdate 3가지가 있습니다. 어떤 상황에 어떤 함수를 호출해야 하는지 알기 위해 각 함수별 특징과 차이점을 설명합니다. Update() - 스크립트가 e

developug.blogspot.com

 

'백지부터 시작하는 이세계 유니티 생활 since 2020' 카테고리의 다른 글

Coroutine, IEnumerator, yield return  (0) 2020.11.17
parameter, argument  (0) 2020.11.17
override, overload  (0) 2020.11.17
Time.deltatime  (0) 2020.11.17
unity 3D movement 구현하기  (0) 2020.11.15
Comments