Avatar Class

Avatar is used to represent a player in a Room.

Definition

Namespace: Alteruna
Assembly: Alteruna.Trinity (in Alteruna.Trinity.dll) Version: 1.3.4
C#
public class Avatar : CommunicationBridge
Inheritance
Object    Object    Component    Behaviour    MonoBehaviour    CommunicationBridge    Avatar

Remarks

Avatar is a CommunicationBridge and can be used to synchronize data between clients.

Example

Avatars have events that can be used similar to OnEnable and OnDisable. The possession status can also be accessed with Avatar.IsPossessed
C#
// Avatar reference
public Avatar MyAvatar;

void Awake() {
    // Event for avatar possessed
    MyAvatar.OnPossessed.AddListener(Possessed);

    // OnUnpossessed is called on unposses and client disconnect.
}

// Log username on Possession
void Possessed(User user) => Debug.Log("Possessed by " + user.Name);
When working with Avatars, the most useful information is to check for the controlled Avatar, this can be done using Avatar.IsMe. To make a fist person game, simply move the camera to the avatar with the Avatar.IsMe set to true. See following example.
C#
public Avatar MyAvatar;

void Awake() {
    MyAvatar.OnPossessed.AddListener(Possessed);
}

void Possessed(User user) {
    // Return if not user's client.
    if (user != MyAvatar.Multiplayer.Me) return;

    // Set camera as child to object
    Camera myCamera = Camera.main;
    myCamera.transform.position = transform.position + new Vector3(0, 0.4f, 0);
    myCamera.transform.SetParent(transform);

    // Lock cursor
    Cursor.lockState = CursorLockMode.Locked;
    Cursor.visible = false;
}

Properties

IsMe True when the Avatar represents the local player.
IsOwner True when the Avatar represents the local player.
OwnerUser that posses the Avatar. Null when unprocessed.
PossessorUser that posses the Avatar. Null when unprocessed.

Methods

Possessed(User) The possessed method can be used to set owner of a avatar.
Possessed(Boolean, User) The possessed method can be used to set owner of a avatar.
(Overrides CommunicationBridgePossessed(Boolean, User))
Serialize Serializes transform and synchronizables.
ToString Get name and index in a string. Returns "Unprocessed" when IsPossessed is false.
(Overrides Object.ToString)
Unpossessed
(Overrides CommunicationBridgeUnpossessed)
Unserialize Unserialize transform and synchronizables.

Fields

IsPossessed True when possessed by a User.
IsPossessor True when the Avatar represents the local player.
OnPossessed On Avatar get posses by new User.
OnUnpossessed On Avatar unpossess.

See Also