我正在构建一个平台,当玩家进入该区域时,区域的颜色会发生变化。这适用于我的测试运行,但在我的构建中,下一个区域的颜色会更改。
具体来说,我的区域是层次结构中的所有精灵,每个都有唯一的标记"成功" +区域的编号。当我在我的构建上运行时,碰撞检测器检测到区域编号比它应该多1个。
构建
生成
我的碰撞脚本:
public class CollisionDetector2 : NetworkBehaviour {
void OnTriggerEnter2D(Collider2D other)
{
if (!isLocalPlayer)
return;
Debug.Log(other.tag);
if (other.tag.Substring(0, 7) == "Success")
{
if (isServer)
{
GameObject.Find("GameController").GetComponent<ProgressTracker> ().Collide(Int32.Parse(other.tag.Substring(7)));
}
else
{
CmdNotifyCollide(Int32.Parse(other.tag.Substring(7)));
}
}
}
void OnTriggerExit2D(Collider2D other)
{
if (!isLocalPlayer)
return;
if (other.tag.Substring(0, 7) == "Success")
{
if (isServer)
{
GameObject.Find("GameController").GetComponent<ProgressTracker>().Exit(Int32.Parse(other.tag.Substring(7)));
}
else
{
CmdNotifyExit(Int32.Parse(other.tag.Substring(7)));
}
}
}
[Command]
void CmdNotifyCollide(int num)
{
GameObject.Find("GameController").GetComponent<ProgressTracker>().Collide(num);
}
[Command]
void CmdNotifyExit(int num)
{
GameObject.Find("GameController").GetComponent<ProgressTracker>().Exit(num);
}
}
我的精灵颜色变化脚本:
public class ProgressTracker : NetworkBehaviour {
public SyncListBool success = new SyncListBool();
void Start()
{
success.Clear();
for (int i = 0; i < 8; i++)
{
success.Add(false);
}
success.Callback = OnSuccessChange;
}
public void Collide(int num)
{
if (!isServer)
return;
//Debug.Log(num);
success[num - 1] = true;
}
public void Exit(int num)
{
if (!isServer)
return;
success[num - 1] = false;
}
public void OnSuccessChange(SyncListBool.Operation op, int index)
{
int total = 0;
for (int i = 0; i < 8; i++)
{
if (success == true)
{
total += 1;
}
}
if (total == 2)
{
NetworkManager.singleton.ServerChangeScene("SeniorProject3");
}
if (success[index])
{
SpriteRenderer block = GameObject.Find("Success" + (index + 1)).GetComponent<SpriteRenderer>();
block.color = new Color32(104, 104, 104, 255);
}
else
{
SpriteRenderer block = GameObject.Find("Success" + (index + 1)).GetComponent<SpriteRenderer>();
block.color = new Color32(182, 182, 182, 255);
}
}
}
答案 0 :(得分:0)
避免使用查找和 GetComponent ,它们是耗时的方法,可能会对物理模拟产生不良影响。
要改善碰撞检测,请更改刚体上的插值模式。