实例化坐标c#
本文关键字:坐标 实例化 | 更新日期: 2023-09-27 18:18:05
我正在做一款带有RPC网络的统一游戏,我想在特定坐标中生成玩家。以下是新玩家刷出代码:'
[RPC]
void JoinPlayer(NetworkViewID newPlayerView, Vector3 pos, NetworkPlayer p)
{
// instantiate the prefab
// and set some of its properties
GameObject newPlayer = Instantiate(playerPrefab, pos, Quaternion.identity) as GameObject;
newPlayer.GetComponent<NetworkView>().viewID = newPlayerView;
newPlayer.tag = "Player";
// set the remote player's target to its current location
// so that non-moving remote player don't move to the origin
newPlayer.GetComponent<playerController>().target = pos;
// most importantly, populate the NetworkPlayer
// structure with the data received from the player
// this will allow us to ignore updates from ourself
newPlayer.GetComponent<playerController>().netPlayer = p;
// the local GameObject for any player is unknown to
// the server, so it can only send updates for NetworkPlayer
// IDs - which we need to translate to a player's local
// GameObject representation
// to do this, we will add the player to the "players" Hashtable
// for fast reverse-lookups for player updates
// Hashtable structure is NetworkPlayer --> GameObject
players.Add(p,newPlayer);
`
那么我如何在特定坐标中生成玩家?
简单地给出对象坐标。
GameObject newPlayer = Instantiate(playerPrefab,
pos,
Quaternion.identity) as GameObject;
使用上面的代码,调用函数Instantiate()
。这个方法的参数是:
static function Instantiate (original : Object,
position : Vector3,
rotation : Quaternion) : Object
position
参数就是它所说的:新创建对象的位置。如果您想看一些示例,请点击这里查看脚本参考。
Vector3 pos = new Vector3(505, 7, 369);