unity2D笔记-触发提示框和跳跃、陷阱平台

在这里插入图片描述

1.提示框面板脚本(挂载到Tips):

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

public enum TipsStyle
{
    Style1,
}

public class TipsPanel :MonoBehaviour
{
    // Start is called before the first frame update
    GameObject styleobj;
    public static TipsPanel _instance;
    private void Awake()
    {
        _instance = this;
    }
    private void Start()
    {
        styleobj = transform.Find("Style1").gameObject;
    }
    public void showTips(string content, TipsStyle style)
    {
        switch (style)
        {
            case TipsStyle.Style1:
                styleobj.SetActive(true);
                styleobj.transform.Find("Content").GetComponent<Text>().text = content;
                break;
        }
    }

    // Update is called once per frame
  public void HideTips(TipsStyle style)
    {
        switch (style)
        {
            case TipsStyle.Style1:
                styleobj.SetActive(false);
                break;
        }
    }
}

2.提示触发器(挂载到InfoSign)

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

public class InfoSign : MonoBehaviour
{
    public Sprite normalSprite, lightSprite;
    SpriteRenderer render;
    public string tipContent;
    // Start is called before the first frame update
    private void Start()
    {
        render = transform.GetComponent<SpriteRenderer>();
    }
    public void OnTriggerEnter2D(Collider2D collider)
    {
        if(collider.tag == "Player")
        {
            render.sprite = lightSprite;
            //显示提示
            TipsPanel._instance.showTips(tipContent, TipsStyle.Style1);
        }
    }

    // Update is called once per frame
    public void OnTriggerExit2D(Collider2D collider)
    {
        if (collider.tag == "Player")
        {
            render.sprite = normalSprite;
            TipsPanel._instance.HideTips(TipsStyle.Style1);
        }
    }
}

3.跳跃平台

使用platform effector 2D实现:
在这里插入图片描述
1.Use Collider Mask:设置可以与哪些层级(layer)发生碰撞。
2.Rotational Offset:碰撞角度的方向。
3.one way:使用一条路可以穿过该碰撞体,不勾选则默认全可以碰撞 Surface Arc:设置碰撞角度。
4.sides:设置碰撞体摩擦力和反弹力。

4.陷阱平台

当小人站到一个平台时,如果该平台是陷阱,则下落。
1.陷阱平台脚本

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

public class PassPlatform : MonoBehaviour
{
    int targetLayer;//下落层
    PlatformEffector2D platformEffector;
    private void Start()
    {
        platformEffector = transform.GetComponent<PlatformEffector2D>();
    }
    // 穿过这个平台
    public void Fall(GameObject obj)
    {
        targetLayer = 1 <<obj.layer;
        platformEffector.colliderMask &= ~targetLayer;//取消当前层
        /*
         LayerMask类
        其中的值为一个32位值。
        层蒙板类,UNITY3D支持0~31共32个层,第0层为default,整数值为-1

        接下来,除第0层(default)外,其余的层都用对应的那一个二进制位表示
        如
        第一层就是00000000000000000000000000000010
        第二层就是00000000000000000000000000000100
         */
        //恢复
        Invoke("ResetLayer", 0.5f);
    }
    public void ResetLayer()
    {
        platformEffector.colliderMask |= ~targetLayer;
    }
}

2.人物检测脚本
如果当前的平台是陷阱平台则触发人物状态机的下落状态,触发函数如下:

 	
    PassPlatform curentPlatform; //当前所处平台
    public  bool CheckFall()
    {
        if (curentPlatform != null)
        {
            curentPlatform.Fall(gameObject);
            return true;
        }
        return false;
    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        curentPlatform = collision.gameObject.GetComponent<PassPlatform>();
    }
    private void OnCollisionExit2D(Collision2D collision)
    {
        curentPlatform = null;
    }
}

3.下落状态脚本

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

public class PlayerFallState : State<PlayerController>
{
    private static PlayerFallState _instance;
    public static PlayerFallState Instance
    {
        get
        {
            if (_instance == null)
                _instance = new PlayerFallState();
            return _instance;
        }
    }

    private PlayerFallState()
    {
    }

    public override void Enter()
    {
        owner.isFall = true;
    }

    public override void Execute()
    {
        #region check Grounded
        if (owner.CheckGrounded())
        {
                owner.StateMachine.ChangeState(PlayerStandState.Instance);
        }
        #endregion
    }

    public override void Exit()
    {
        owner.isFall = false;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值