Legacy Documentation: Version 5.4
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

CommandAttribute

class in UnityEngine.Networking

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

Sumbission failed

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

Close

Cancel

Description

This is an attribute that can be put on methods of NetworkBehaviour classes to allow them to be invoked on the server by sending a command from a client.

[Command] functions are invoked on the player object associated with a connection. This is setup in response to the "ready" message, by passing the player objec to the NetworkServer.PlayerIsReady() function. The arguments to the command call are seriialized across the network, so that the server function is invoked with the same values as the function on the client. These functions must begin with the prefix "Cmd".

#pragma strict
public class Player extends NetworkBehaviour {
	var moveX: int = 0;
	var moveY: int = 0;
	var moveSpeed: float = 0.2f;
	function Update() {
		if (!isLocalPlayer) {
			return ;
		}
		// input handling for local player only
		var oldMoveX: int = moveX;
		var oldMoveY: int = 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
	public function CmdMove(x: int, y: int) {
		moveX = x;
		moveY = y;
		isDirty = true;
	}
	public function FixedUpdate() {
		if (NetworkServer.active) {
			transform.Translate(moveX * moveSpeed, moveY * moveSpeed, 0);
		}
	}
}
using UnityEngine;
using UnityEngine.Networking;

public class Player : NetworkBehaviour {

int moveX = 0; int moveY = 0; float moveSpeed = 0.2f; 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] public void CmdMove(int x, int y) { moveX = x; moveY = y; isDirty = true; } public void FixedUpdate() { if (NetworkServer.active) { transform.Translate(moveX * moveSpeed, moveY * moveSpeed, 0); } } }

Variables

channelThe QoS channel to use to send this command on, see QosType.