如何在将网络起始位置添加到游戏对象后修复触发器



你好,我是使用unity和C#的新手。我目前正在为一个项目制作一个游戏,我使用了吃豆人教程并使其成为多人游戏。我已经设法让多人游戏部分正常工作,但是,一旦我添加了两个具有网络起始位置的游戏对象并在生成信息下添加到网络管理器中,我的触发器突然停止工作。我的玩家对象应该使吃豆人圆点在与它碰撞时消失,我的玩家对象在与吃豆人幽灵碰撞时应该消失。谁能告诉我发生了什么?

我的玩家对象:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class UFOMove : NetworkBehaviour 
{
public float speed;             
private Rigidbody2D rb2d;      
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
}
void Update()
{
if (!isLocalPlayer)
{
return;
}
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");       
float moveVertical = Input.GetAxis("Vertical");      
Vector2 movement = new Vector2(moveHorizontal, moveVertical);       
rb2d.AddForce(movement * speed);
}
}

我的幽灵运动:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GhostMove : MonoBehaviour 
{
public Transform[] waypoints;
int cur = 0;
public float speed = 0.3f;
void FixedUpdate()
{
if (transform.position != waypoints[cur].position)
{
Vector2 p = Vector2.MoveTowards(transform.position,
waypoints[cur].position,
speed);
GetComponent<Rigidbody2D>().MovePosition(p);
}
else 
cur = (cur + 1) % waypoints.Length;
Vector2 dir = waypoints[cur].position - transform.position;
GetComponent<Animator>().SetFloat("DirX", dir.x);
GetComponent<Animator>().SetFloat("DirY", dir.y);
}
void OnTriggerEnter2D(Collider2D co)
{
if (co.name == "UFO")
Destroy(co.gameObject);
}
}

吃豆人点:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PacDot : MonoBehaviour 
{
void OnTriggerEnter2D(Collider2D co)
{
if (co.name == "UFO")
Destroy(gameObject);
}
}

我刚刚意识到这个问题。回答我自己的问题,以防其他人遇到麻烦。在游戏上添加多人游戏功能会更改玩家对象的名称。对我来说,我的UFO每次在本地主机和客户端上运行时都会将其名称更改为UFO(克隆(。因此,我没有在 TriggerEnter 上查找对象的名称,而是更改了代码,以便它查找标记。我向我的播放器对象添加了一个标签,以便可以找到它。

void OnTriggerEnter2D(Collider2D co)
{
if (co.tag == "UFO")
Destroy(co.gameObject);
}

最新更新