【问题标题】:instantiate coordinates C#实例化坐标 C#
【发布时间】:2013-06-13 10:01:13
【问题描述】:

我正在使用 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);
    `

那么我怎样才能在特定坐标中生成玩家呢?

【问题讨论】:

    标签: c# networking unity3d


    【解决方案1】:

    只需给出对象坐标。

    GameObject newPlayer = Instantiate(playerPrefab, 
                                       pos, 
                                       Quaternion.identity) as GameObject;
    

    使用上面的代码,调用函数Instantiate()。该方法的参数为:

    static function Instantiate (original : Object, 
                                 position : Vector3, 
                                 rotation : Quaternion) : Object 
    

    position 参数正是它所说的:新创建对象的位置。如果你想看一些例子,click here for the Script Reference

    因此,例如,如果您有坐标 505、7、369,则可以在实例化代码上方添加以下代码行:

    Vector3 pos = new Vector3(505, 7, 369);
    

    【讨论】:

    • 好吧,我只是一个初学者,那么我的代码到底应该是什么样子?假设坐标是 505、7、369
    猜你喜欢
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-06
    • 2011-07-19
    • 2016-06-27
    相关资源
    最近更新 更多