尽管可能不受Unity对象的影响,但Unity对象与在其后创建的克隆发生冲突 [英] Unity object collides with clone created behind it despite supposedly not being affected by it
问题描述
我已按照本教程制作了tron游戏: https://noobtuts. com/unity/2d-tron-lightcycles-game 然后尝试使用此功能添加多人游戏功能
I have followed this tutorial to make a tron game: https://noobtuts.com/unity/2d-tron-lightcycles-game and then attempted to add multiplayer capability using this
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;
}
}
}
每当一条消息从手机(客户端)发送到显示游戏并更改每个玩家方向的服务器时就会调用.
which is called every time a message is sent from the phone (client) through to the server which is displaying the game and changes the direction of each player.
但是这样做似乎破坏了游戏的常规功能:
However in doing so I seem to have broken the regular function of the game:
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);
}
这两个函数负责创建跟随第一个对象的墙,以创建Tron的基础.然后在这里使用死亡检查方法:
These two functions are responsible for creating a wall that follows the first object to create the basis of Tron. Then in the death check method here:
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision != wall)
{
Debug.Log("deading because of " + collision.name);
//add losing stuff
Destroy(gameObject);
}
}
它可以防止它与墙壁碰撞.但是,现在无论何时接收到输入,动作都会在很短的时间内发生,然后显然与它后面的一个克隆对象发生冲突.
it prevents it from colliding with the wall. However, now whenever an input is received, the action will happen for a very short time then apparently collide with one of the cloned objects behind it.
我尝试将spawnWall函数延迟无济于事,如果延迟时间超过0.3秒,它只会这样做:
I tried delaying the spawnWall function to no avail, it just did this if delayed any longer than 0.3 seconds:
在更新内调用createConstantWall方法.每当从客户端接收到输入时,都会调用MovePlayer方法.
The createConstantWall method is called within update. The MovePlayer method is called whenever an input is received from the client.
推荐答案
添加一个变量,该变量阻止每次调用spawnWall方法时都会启用的碰撞检查,似乎已经修复了该问题.
Adding a variable that stops the collision check from being called that gets enabled every time the spawnWall method is called for a second seems to have fixed it.
这篇关于尽管可能不受Unity对象的影响,但Unity对象与在其后创建的克隆发生冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!