C# Unity对象与在其后面创建的克隆发生冲突,尽管据推测不受其影响
我按照本教程制作了一个tron游戏: 然后尝试使用此功能添加多人游戏功能C# Unity对象与在其后面创建的克隆发生冲突,尽管据推测不受其影响,c#,unity3d,C#,Unity3d,我按照本教程制作了一个tron游戏: 然后尝试使用此功能添加多人游戏功能 void MovePlayer(int inputPlayerId, string direction) { Debug.Log("Attempting Move on " + inputPlayerId + " " + direction); if (inputPlayerId != playerId) return; else { switch (dire
void MovePlayer(int inputPlayerId, string direction)
{
Debug.Log("Attempting Move on " + inputPlayerId + " " + direction);
if (inputPlayerId != playerId)
return;
else
{
switch (direction)
{
case "N":
Debug.Log("moving up");
GetComponent<Rigidbody2D>().velocity = Vector2.up * speed;
spawnWall();
break;
case "E":
Debug.Log("moving right");
GetComponent<Rigidbody2D>().velocity = Vector2.right * speed;
spawnWall();
break;
case "S":
Debug.Log("moving down");
GetComponent<Rigidbody2D>().velocity = -Vector2.up * speed;
spawnWall();
break;
case "W":
Debug.Log("moving left");
GetComponent<Rigidbody2D>().velocity = -Vector2.right * speed;
spawnWall();
break;
}
}
}
它可以防止它与墙壁相撞。然而,现在每当收到一个输入时,这个动作都会在很短的时间内发生,然后显然会与它后面的一个克隆对象发生碰撞
我尝试延迟spawnWall函数,但没有效果,如果延迟超过0.3秒,它就会这样做:
在update中调用createConstantWall方法。每当从客户端接收到输入时,都会调用MovePlayer方法。添加一个变量,该变量可阻止每次调用spawnWall方法一秒钟时启用的碰撞检查,这似乎已经解决了该问题。您的
spawnWall
方法总是在播放器的正上方生成一堵墙,不在他们后面。除此之外,还有两个更新周期,update
(每个图形渲染帧)和fixeupdate
(每个物理更新帧;碰撞始终在fixeupdate周期中处理)。你能用显示的碰撞器大小显示游戏场景吗?每次调用spawnwall时,我都会在碰撞器检查上添加一个延迟,它似乎工作正常。谢谢。
public void spawnWall()
{
lastWallEnd = transform.position;
GameObject objectOfGame = (GameObject)Instantiate(wallPrefab, transform.position, Quaternion.identity);
wall = objectOfGame.GetComponent<Collider2D>();
}
void createConstantWall(Collider2D collision, Vector2 start, Vector2 finish)
{
collision.transform.position = start + (finish - start) * 0.5f;
float distance = Vector2.Distance(start, finish);
if (start.x != finish.x)
collision.transform.localScale = new Vector2(distance + 1, 1);
else
collision.transform.localScale = new Vector2(1, distance + 1);
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision != wall)
{
Debug.Log("deading because of " + collision.name);
//add losing stuff
Destroy(gameObject);
}
}