아미(아름다운미소)

[Unity] 2D 점프 본문

랭귀지/Unity

[Unity] 2D 점프

유키공 2018. 4. 5. 09:30

Unity 2D 점프

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

public class Jump : MonoBehaviour { 
    Rigidbody2D rgdy; 
    public float JP = 10f; 
    bool isJumping = false; 

// Use this for initialization 
void Start () { 
        rgdy = gameObject.GetComponent<Rigidbody2D>(); 
} 

// Update is called once per frame 
void Update () { 
        if(Input.GetKeyDown(KeyCode.Space)) 
        isJumping = true; 
} 

    private void FixedUpdate() 
    { 
        Jumping(); 
    } 

    void Jumping() 
    { 
        if (!isJumping) 
            return; 

        rgdy.velocity = Vector2.zero; 

        Vector2 jumpVelocity = new Vector2(0, JP); 
        rgdy.AddForce(jumpVelocity, ForceMode2D.Impulse); 

        isJumping = false; 
    } 
}
Comments