public bool isLocalPlayer ;

Descripción

Devuelve true si este objeto es el que representa al jugador en la máquina local.

En juegos multijugador, hay múltiples instancias del objeto Player. El cliente necesita saber cuál de ellas es "de él" para que solo ese jugador procese la entrada y posiblemente también tenga añadida una cámara. La función IsLocalPlayer devolverá true solo para la instancia del jugador en la máquina local, para que pueda ser usada al filtrar la entrada en jugadores no locales.

Este código de ejemplo muestra un procesamiento de entrada que afectará únicamente al jugador local.

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