본문 바로가기

유니티 3D기본(Roll A Ball)

13. 공 굴리기

간단하게 공과 바닥 오브젝트 생성하고 각각의 Material을 만들어 줬습니다.

그리고 C# 스크립트를 생성해서 공에게 줬습니다.

소스코드

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

public class PlayerBall : MonoBehaviour
{
    Rigidbody rigid; //rigid 컴포넌트 가져오기

    void Awake()
    {
        rigid = GetComponent<Rigidbody>(); //초기화        
    }

    void FixedUpdate()
    {
        float x = Input.GetAxisRaw("Horizontal");
        float z = Input.GetAxisRaw("Vertical");
        Vector3 move = new Vector3(x, 0, z);

        rigid.AddForce(move, ForceMode.Impulse);

    }

}

결과