개인적인 공부/Unity

C# void Start(), void Update()

karatejin 2023. 1. 5. 23:48

What are Method?

 

Methods (also called Functions) execute blocks of code that makes our game do things

We can:

Use the methods already available in Unity

Create our own methods

 

Creating And Calling

When we CREATE a method, we are giving it a name and saying what if should do.

 

 

처음에 C# Script를 생성하면 나오는 것.

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

public class Driver : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

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

void Start() : 시작하자마자.

transform Rotation 에 z 축 을 45도 기울인다.

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

public class Driver : MonoBehaviour
{
    void Start()
    {
        transform.Rotate(0,0,45);
    }

    void Update()
    {
        
    }
}

결과 시작하면 화면에  Jacks Car sprite가 기울어진다.

void Update() 프레임 마다 출력한다.

 

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

public class Driver : MonoBehaviour
{
    void Start()
    {
        
    }

    void Update() //프레임마다 호출된다.
    {
        transform.Rotate(0,0,45);        
    }
}

 

void Update() 는 프레임 마다 호출된다.

45 에서 0.1 f 로 변경

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

public class Driver : MonoBehaviour
{
    void Start()
    {
        
    }

    void Update() //프레임마다 호출된다.
    {
        transform.Rotate(0,0,0.1f);        
    }
}

 

속도변경