본문 바로가기
Unity

[유니티] 3D 큐브 생성 + 회전하기

by Skull Crusher 2021. 4. 21.
728x90

 

1. 3D 큐브 추가

GameObject -> 3D Object -> Cube 클릭!!

Inspector 에서 큐브 이름 바꿔기 (RedCube, YellowCube, BlueCube)

2. 큐브에 색 넣기 

Assets -> 마우스오른쪽 클릭 -> Create -> Folder -> Materials 폴더 만들기

Materials 폴더에서 마우스오른쪽 클릭 -> Create -> Material 클릭

Inspector Albedo 클릭하고 원하는 색 지정

Materials 색을 큐브에 드레그

3. C# Script 만들기

Assets -> 마우스오른쪽 클릭 -> Create -> Folder -> Script 폴더 만들기

Script 폴더에서 마우스오른쪽 클릭 -> Create -> C# Script 클릭 ->파일이름 : RefTransformComponet

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

public class RefTransformComponet : MonoBehaviour
{
    // Transform 클래스 타입
    // trComponentRef <- 참조변수
    // * Transform 객체가 생성되는게 아님!!!
    public Transform trComponentRef;

    // float 변수 생성
    public float speed;

    // Transform 클래스 타입
    // -> 노란색 큐브의 Transform 컴포넌트(객체) 참조 변수
    public Transform yellowCubeTrRef;

    // 현재 컴포넌트를 가진 게임오브젝트가 렌더링 될때마다 호출되는
    // 이벤트 메소드
    void Update()
    {       

        // trComponentRef 참조변수에 참조된 객체의 Rotate 메소드 실행!!
        trComponentRef.Rotate(Vector3.up * speed * Time.deltaTime);

        yellowCubeTrRef.Rotate(Vector3.right * speed * Time.deltaTime);
    }
}

Inspector에 Transform을 첫번째 None(Transform) 에 드레그하고, 차례로 YellowCube, BlueCube 드레그하고,

Speed에 90 입력

완성 모습!!

Play를 누르면 Game 화면에서 큐브가 회전하는 모습을 볼 수 있다 :)

(큐브 위치를 내가 원하는 곳에 둘 수도 있다)

 

유야호~~~~

'Unity' 카테고리의 다른 글

[유니티] 회전하기  (0) 2021.05.14
[유니티] 3D 캐릭터 이동하기  (0) 2021.05.13
[유니티] 효과 넣기  (0) 2021.05.06
[유니티] 3D 점수 계산  (0) 2021.05.06
[유니티] 지정위치에 랜덤 오브젝트 놓기  (0) 2021.04.24

댓글