앱 만들기/게임

유니티 - 플레이어에게 힘을 주기

나도 처음이야 2019. 1. 30.

유니티 - 플레이어에게 힘을 주기


Rigidbody2D 컴포넌트의 Addforce 메서드를 사용하면 된다.



using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class PlayerController : MonoBehaviour

{

    Rigidbody2D rigid2D;

    float jumpforce = 300.0f;


    // Start is called before the first frame update

    void Start()

    {

        this.rigid2D = GetComponent<Rigidbody2D>();


    }


    // Update is called once per frame

    void Update()

    {

        if (Input.GetKeyDown(KeyCode.Space))

        {

                this.rigid2D.AddForce(Vector3.up/*transform.up*/ * this.jumpforce);

        }   

    }

}

반응형

댓글