unity传人物位置给服务器,把人物用 Unity 进行 2D 传送,拢共分四步|原力计划...

46d413f90404c0936327f9bab66c9218.png

作者 | 珞珈大胖强TURBO

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

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

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

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

传送门检测人物进入

62df265f4cb1663bdfffe0d229943bf0.png

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

1privatevoidOnTriggerEnter2D(Collider2D collision)2{3if ( collision.gameObject.layer == 8)4 }

其实这两行代码就解决问题了,也就是在OnTriggerEnter2D检测碰撞体的图层是不是人物图层,这里是8的原因是因为我的项目的人物图层是8.

获得目的传送门的 GameObject

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

1publicenum KindofGate2 {3 Gate1,4 Gate2,5 Gate3,6 Gate47 }89public KindofGate kindofGate;

这些代码,设置传送门种类枚举,创建枚举对象kindofGate,自己设置想要互相传送的传送门kindofGate相同

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

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

1List portalGateways; 2 portalGateways = newList(); 3 public staticvoid RegistPortalGateway(PortalGateway portalGateway) 4 { 5if (!instance.portalGateways.Contains(portalGateway)) 6 instance.portalGateways.Add(portalGateway); 7 } 8``

2. 每个传送门的代码的Start部分调用,将自身注册(也就是加入进List)。

1```c2 GameManager.RegistPortalGateway(this);

3. 在GameManager中加入寻找方法,参数为PortalGateway对象,遍历List,寻找和该对象的kindofGate相同的对象。

1publicstatic PortalGateway FindSameGate(PortalGateway portalGateway) 2{ 3 4foreach(PortalGateway portalGateway1 in instance.portalGateways) 5 { 6if(portalGateway1!=portalGateway) 7 { 8if (portalGateway1.kindofGate == portalGateway.kindofGate) 9return portalGateway1;10 }11 }12returnnull;13 }

4. 在PortalGateway中调用方法,寻找到相同种类的传送门

1GameManager.FindSameGate(this)

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

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

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

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

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

重新开启传送功能

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

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

1publicbool CanCheckAnother; 2//进 3privatevoidOnTriggerEnter2D(Collider2D collision) 4{ 5if ( collision.gameObject.layer == 8) 6 { 7 8if (CanCheckAnother == true) 9 {1011if (GameManager.FindSameGate(this).CanCheckAnother == true)12 {13 collision.transform.position = GameManager.FindSameGate(this).transform.position;14 CanCheckAnother = false;15 } 16 }1718 }19 }20//出 21privatevoidOnTriggerExit2D(Collider2D collision)22{23if (collision.gameObject.layer == 8 )24 GameManager.FindSameGate(this).CanCheckAnother = true;25 }

总代码如下:

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5publicclassPortalGateway : MonoBehaviour 6{ 7publicenum KindofGate 8 { 9 Gate1,10 Gate2,11 Gate3,12 Gate413 }1415public KindofGate kindofGate;16publicbool CanCheckAnother;1718privatevoidStart()19{2021 CanCheckAnother = true;2223 GameManager.RegistPortalGateway(this);2425 }2627privatevoidOnTriggerEnter2D(Collider2D collision)28{29if ( collision.gameObject.layer == 8)30 {3132if (CanCheckAnother == true)33 {3435if (GameManager.FindSameGate(this).CanCheckAnother == true)36 {37 collision.transform.position = GameManager.FindSameGate(this).transform.position;38 CanCheckAnother = false;39 } 40 }4142 }4344454647 }484950privatevoidOnTriggerExit2D(Collider2D collision)51{52if (collision.gameObject.layer == 8 )53 GameManager.FindSameGate(this).CanCheckAnother = true;54 }5556}

原文链接:

https://blog.csdn.net/weixin_44739495/article/details/104742250

举报/反馈

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值