상세 컨텐츠

본문 제목

유니티 버튼 누르면 큐브 생성하기

유니티/기능

by MJ_119 2024. 9. 29. 12:53

본문

- 버튼 누르면 큐브 생성하기

 B 버튼 클릭하면 큐브 생성해서 내가 만든 mergeTest.cs 스크립트 컴포넌트 추가하기.

using UnityEngine;

public class playerInput : MonoBehaviour
{
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.B))
        {
            GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
            
            cube.AddComponent<mergeTest>();
        }
    }
}

 

- mergeTest.cs

using UnityEngine;

public class mergeTest : MonoBehaviour
{
    void Update()
    {
        Vector3 playerPosition = GameObject.FindGameObjectWithTag("Player").transform.position;

        transform.position += Vector3.forward * Time.deltaTime;
    }
}

 

관련글 더보기