Version: 5.6
public bool isLocalPlayer ;

설명

This returns true if this object is the one that represents the player on the local machine.

In multiplayer games, there are multiple instances of the Player object. The client needs to know which one is for "themselves" so that only that player processes input and potentially has a camera attached. The IsLocalPlayer function will return true only for the player instance that belongs to the player on the local machine, so it can be used to filter out input for non-local players.

This example shows processing input for only the local player.

public class Player : NetworkBehaviour
{
    int moveX = 0;
    int moveY = 0;

void Update() { if (!isLocalPlayer) { return; }

// input handling for local player only int oldMoveX = moveX; int oldMoveY = moveY;

moveX = 0; moveY = 0;

if (Input.GetKey(KeyCode.LeftArrow)) { moveX -= 1; } if (Input.GetKey(KeyCode.RightArrow)) { moveX += 1; } if (Input.GetKey(KeyCode.UpArrow)) { moveY += 1; } if (Input.GetKey(KeyCode.DownArrow)) { moveY -= 1; } if (moveX != oldMoveX || moveY != oldMoveY) { CmdMove("Move", moveX, moveY); } }

[Command] void CmdMove(int dx, int dy) { // move here } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961