아미(아름다운미소)

[Unity] 2D 점프 본문

랭귀지/Unity

[Unity] 2D 점프

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

Unity 2D 점프

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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