用最简单的方式实现进门效果(走到门前点击门,门打开后走进去门再关上)

本文详细介绍了一种在Unity3D游戏中实现角色与门互动的过程,包括距离检测、鼠标点击检测、动画控制、遮罩层级调整等步骤,以及相关代码示例。
摘要由CSDN通过智能技术生成

整体思路

1、先检测玩家和门的距离当小于一定距离之后提示玩家点击门
2、检测到玩家点击门(射线检测)后门被打开
3、玩家走进去,改变遮罩层级用门把玩家遮住
4、当玩家走进去之后关门
开关门特效使用动画机(animation)鼠标点击使用射线检测(ray)

实现

1、准备

找一个开关门的素材然后用Sprite Editor切割:
图片切割
把一个图片拖到场景中
在这里插入图片描述
为这个图片添加animation,三个状态:开门、关门、常闭
在这里插入图片描述
编辑animator(因为我设定当角色超过一定等级的时候才能打开,所以加了一个level变量,如果只是单纯的想开关门那就不用)
三个状态转移的判定条件分别是
stay->open: open(true) close(false) level(greater 6)
open->close: open(false) close(true)
close->stay: stay(true)
在这里插入图片描述
添加点击门的提示(同样用animation实现这里就不再赘述),拖到assets里面作为预制体(其实托不拖都可以,我主要是考虑到其他地方可能可以用到)
在这里插入图片描述
然后先把它隐藏(inspector面板的这个勾勾勾掉)
在这里插入图片描述

2、实现

分为几个部分:①检测距离 ②鼠标点击检测 ③控制动画机 ④改变遮罩层级⑤小细节
(1)距离检测
距离检测直接用官方API提供的距离检测方法:

var heading = player.transform.position - this.transform.position;
float distance = heading.magnitude;

获得distance之后检测距离当玩家在范围内才会进行鼠标检测
(2)鼠标点击检测
由于游戏场景中的门不是按钮,所以检测鼠标点击我选择用射线检测。

if (Input.GetMouseButtonDown(0))//鼠标点击事件
{
	//射线
	Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit2D hit = Physics2D.Raycast(ray.origin, Vector2.down);
    if (hit.collider && hit.collider.tag == "goods")
    {
    	//TODO
    }
}

(3)控制动画机
这个就是常规方法在对应时机设置动画机参数就可以自动播放了,用到的函数:

animator.SetBool("变量名",);
animato.setInteger("变量名",);
animato.setFloat("变量名",);
....

(4)改变层级
有人会问到这里不就差不多结束了吗为啥还要改变这个?是因为如果不改变的话当人物走进去门关上之后人物始终在门的前面,像这样:
在这里插入图片描述
所以当人物走进去之后为了达到门把人关在里面的错觉就需要让门盖住人。(人物的渲染层级我设置的是2,比他高一级就可以了)

SpriteRenderer sr = transform.GetComponent<SpriteRenderer>();
sr.sortingOrder = 3;//层级设置为3

(5)小细节
其实到目前为止基本上可以结束了,但是有个问题还需要小小完善一下,就是我需要在人物没有点击的时候门把人物挡住,在开门之后人物顺利进去。
想实现这个其实超级简单,就是在最开始的时候加一个collider,当门打开之后把collider变成trigger:

BoxCollider2D coll = transform.GetComponent<BoxCollider2D>();
coll.isTrigger = true;

哦对了还有一个点击提示我给忘了,这个更简单就是在玩家靠近的时候setactive变成true 否则变成false(但是别忘了在inspector里面把这个预制体拖到代码里去)

整体效果

开关门

整体代码

	Animator animator;
    public MainCharacter mainCha;
    public GameObject player;
    public GameObject clickAnimation;
    // Start is called before the first frame update
    void Start()
    {
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        var heading = player.transform.position - this.transform.position;
        float distance = heading.magnitude;
        if (distance < 2.5f)
        {
            clickAnimation.SetActive(true);
            if (Input.GetMouseButtonDown(0))
            {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit2D hit = Physics2D.Raycast(ray.origin, Vector2.down);
                if (hit.collider && hit.collider.tag == "goods")
                {
                    if (mainCha.mainChaLevel > 6)
                    {
                        animator.SetBool("open", true);
                        animator.SetBool("stay", false);
                        animator.SetInteger("level", mainCha.mainChaLevel);
                        animator.SetBool("close", false);
                        BoxCollider2D coll = transform.GetComponent<BoxCollider2D>();
                        coll.isTrigger = true;
                    }
                }
            }
        }
        else clickAnimation.SetActive(false);
        if (distance < 0.8f)
        {
            animator.SetBool("open", false);
            animator.SetBool("close", true);
            SpriteRenderer sr = transform.GetComponent<SpriteRenderer>();
            sr.sortingOrder = 3;
            //TODO 结束动画
        }
    }

射线检测引用文章链接:https://blog.csdn.net/weixin_43673589/article/details/117222476

  • 20
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值