Unity3D控制人物跳跃

Unity3D控制人物跳跃_第1张图片
Paste_Image.png

脚本如下:

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour
{

    public float m_JumpSpeed = 400f;
    private bool m_jumping = false;
    private Rigidbody2D m_Rigidbody2D;
    private Animator m_Anim;
    //是否在地面上
    private bool grounded = true;


    private void Awake()
    {
        m_Anim = GetComponent();
        m_Rigidbody2D = GetComponent();
    }

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetButtonDown("Jump"))
        {
            Jump();
        }
    }

    private void Jump()
    {
        if (this.grounded)
        {
            m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpSpeed));
            grounded = false;
        }
    }
    //这个时候在地面上
    private void OnCollisionEnter2D(Collision2D collision)
    {
        grounded = true;
    }
}

你可能感兴趣的:(Unity3D控制人物跳跃)