【从零开始unity2d游戏】4-精灵跳跃及摩擦碰撞

2021/10/24 unity

1、跳跃

新建一个button(右键–>UI–>button)

image-20211115194000715

然后我们新建一个脚本,PlayerSkill,并将这个脚本挂载到我们的button上

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

public class PlayerSkill : MonoBehaviour
{
    public float forceX;
    public float forceY;

   
    public void SetText(string name)
    {
        Text txt = transform.Find("Text").GetComponent<Text>();
        txt.text = name;
    }

    public void Jump(Rigidbody2D playerRb)
    {
        playerRb.AddForce(new Vector2(forceX,forceY));
    }
 
}

然后把button拖过去之后,然后选择对应的函数,之后把我们的Player当做参数,传入到这个函数即可

image-20211115194513410

image-20211115194425103

关于如何使用button按钮,可以参考如下视频:

2、跳跃优化–加速下落

直接调大Player的重力即可

image-20211115195036352

3、跳跃优化–跳跃检测

关于跳跃检测,主要是为了精灵仅能够进行单次的跳跃。按照现在程序,我们的精灵可以不断跳跃,不符合我们的预期,现在我们需要进行改造。

4、跳跃优化–挂靠问题

这个问题可以有两个方案:

  • 使用capsule collider 2d 替换 box collider 2d
  • 降低材质摩擦力

Search

    公众号:豆仔gogo

    豆仔gogo

    Post Directory