找回密码
 立即注册
查看: 363|回复: 0

[笔记] 把人物用 Unity 进行 2D 传送,拢共分四步

[复制链接]
发表于 2023-2-22 17:38 | 显示全部楼层 |阅读模式


作者 | 珞珈大胖强TURBO
来源 | CSDN博客,责编 | 夕颜
封面图 | CSDN 下载自视觉中国
出品 | CSDN(ID:CSDNnews)

首先,假设传送门是两两之间可以传送,那么具体是实现,重要的点有以下四点:


  • 传送门检测人物进入
  • 传送门得到目的传送门(也就是和当前传送门相同种类的门)的GameObject
  • 传送门代码控制人物的位置到目的传送门
  • 暂时关闭目的传送门的传送功能,当人物走出去之后再重新开启传送功能
接下来逐个攻破:



传送门检测人物进入



此图中的圆圈为传送门,给他加C#脚本,命名为PortalGateway

1private void OnTriggerEnter2D(Collider2D collision)
2    {
3        if ( collision.gameObject.layer == 8)
4    }
其实这两行代码就解决问题了,也就是在OnTriggerEnter2D检测碰撞体的图层是不是人物图层,这里是8的原因是因为我的项目的人物图层是8.





获得目的传送门的 GameObject

传送门得到目的传送门(也就是和当前传送门相同种类的门)的GameObject。

1 public enum KindofGate
2    {
3        Gate1,
4        Gate2,
5        Gate3,
6        Gate4
7    }
8
9    public KindofGate kindofGate;
这些代码,设置传送门种类枚举,创建枚举对象kindofGate,自己设置想要互相传送的传送门kindofGate相同



那么,一个传送门怎么找到相同种类的传送门呢?

1. 在GameManager中加入注册方法,统计注册所以的PortalGateway为一个List。

1 List<PortalGateway> portalGateways;
2 portalGateways = new List<PortalGateway>();
3 public static void RegistPortalGateway(PortalGateway portalGateway)
4    {
5        if (!instance.portalGateways.Contains(portalGateway))
6            instance.portalGateways.Add(portalGateway);
7    }
8``
2. 每个传送门的代码的Start部分调用,将自身注册(也就是加入进List)。

1```c
2  GameManager.RegistPortalGateway(this);
3. 在GameManager中加入寻找方法,参数为PortalGateway对象,遍历List,寻找和该对象的kindofGate相同的对象。

1  public static  PortalGateway FindSameGate(PortalGateway portalGateway)
2    {
3
4            foreach(PortalGateway portalGateway1 in instance.portalGateways)
5            {
6                if(portalGateway1!=portalGateway)
7                {
8                if (portalGateway1.kindofGate == portalGateway.kindofGate)
9                    return portalGateway1;
10                }
11            }
12        return null;
13    }
4. 在PortalGateway中调用方法,寻找到相同种类的传送门

1GameManager.FindSameGate(this)
(Gamemanage就是个一个场景自始至终只有一次生成的代码,不会因为关卡重启而删除),具体实现可以学习单例模式,代码就是这种

1 private void Awake()
2    {
3
4        if (instance != null)
5        {
6            Destroy(gameObject);
7            return;
8        }
9
10        instance = this;
11
12        DontDestroyOnLoad(this);
13    }


传送门代码控制人物的位置到目的传送门

有了上面的东西,现在就简单了

1  collision.transform.position = GameManager.FindSameGate(this).transform.position;


重新开启传送功能

暂时关闭目的传送门的传送功能,当人物走出去之后再重新开启传送功能。

设置一个bool状态,控制传送功能:

1  public bool CanCheckAnother;
2  //进
3  private void OnTriggerEnter2D(Collider2D collision)
4    {
5        if ( collision.gameObject.layer == 8)
6        {
7
8            if (CanCheckAnother == true)
9            {
10
11                if (GameManager.FindSameGate(this).CanCheckAnother == true)
12                {
13                    collision.transform.position = GameManager.FindSameGate(this).transform.position;
14                    CanCheckAnother = false;
15                }
16            }
17
18        }
19        }
20      //出  
21    private void OnTriggerExit2D(Collider2D collision)
22    {
23        if (collision.gameObject.layer == 8 )
24            GameManager.FindSameGate(this).CanCheckAnother = true;
25    }
总代码如下:

1using System.Collections;
2using System.Collections.Generic;
3using UnityEngine;
4
5public class PortalGateway : MonoBehaviour
6{
7    public enum KindofGate
8    {
9        Gate1,
10        Gate2,
11        Gate3,
12        Gate4
13    }
14
15    public KindofGate kindofGate;
16    public bool CanCheckAnother;
17
18    private void Start()
19    {
20
21        CanCheckAnother = true;
22
23        GameManager.RegistPortalGateway(this);
24
25    }
26
27    private void OnTriggerEnter2D(Collider2D collision)
28    {
29        if ( collision.gameObject.layer == 8)
30        {
31
32            if (CanCheckAnother == true)
33            {
34
35                if (GameManager.FindSameGate(this).CanCheckAnother == true)
36                {
37                    collision.transform.position = GameManager.FindSameGate(this).transform.position;
38                    CanCheckAnother = false;
39                }
40            }
41
42        }
43
44
45
46
47    }
48
49
50    private void OnTriggerExit2D(Collider2D collision)
51    {
52        if (collision.gameObject.layer == 8 )
53            GameManager.FindSameGate(this).CanCheckAnother = true;
54    }
55
56}
原文链接:
https://blog.csdn.net/weixin_44739495/article/details/104742250

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-4-29 06:38 , Processed in 0.090354 second(s), 27 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表