Version: 2018.4
LanguageEnglish
  • C#
Method group is Obsolete

NetworkBehaviour.isLocalPlayer

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Obsolete The high level API classes are deprecated and will be removed in the future. public bool isLocalPlayer;

Description

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.

using UnityEngine;
using UnityEngine.Networking;

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(moveX, moveY); } }

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