把人物用 Unity 进行 2D 传送,拢共分四步 | 原力计划

作者 | 珞珈大胖强TURBO

来源 | CSDN博客,责编 | 夕颜

封面图 | CSDN 下载自视觉中国

出品 | CSDN(ID:CSDNnews)

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

  1. 传送门检测人物进入

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

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

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

接下来逐个攻破:

传送门检测人物进入

此图中的圆圈为传送门,给他加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

推荐阅读 

李彦宏:百度今年全员涨薪,比去年还多;谷歌暂停Chrome浏览器更新;Eclipse 4.15发布 | 极客头条

GitHub 移动端正式发布!

检测、量化、追踪新冠病毒,基于深度学习的自动CT图像分析有多靠谱?

深度学习“三巨头”、图灵奖得主 Yann LeCun:我没有天赋,所以才追随聪明人

Docker 开发环境的滑坡

来,让我们逐一澄清以太坊 2.0 五大误解

你点的每一个在看,我认真当成了喜欢

发布了1855 篇原创文章 · 获赞 4万+ · 访问量 1674万+

猜你喜欢

转载自blog.csdn.net/csdnnews/article/details/104981103