Prop
Animations
Adding Network-Synced Animations
Adding networked animations for props, as well as worlds and avatars, is quite simple as they can be created using the Unity Animator.
The example code below shows example code for a network-synced Opticor rifle, and the corresponding Animator state machine and triggers it uses:
using UnityEngine;
using System.Collections;
using Basis.Scripts.BasisSdk.Interactions;
using Basis.Scripts.Device_Management.Devices;
using Basis.Shims;
using Basis.Network.Core;
using Basis;
[Cilboxable]
public class OpticorShooting : MonoBehaviour
{
private BasisPickupInteractable pickup;
private BasisNetworkShim network;
private Animator animator;
public AudioSource audioSource;
public AudioClip audioClipCharge;
public AudioClip audioClipMisfire;
void Start()
{
this.pickup = GetComponent<BasisPickupInteractable>();
this.pickup.OnInteractStartEvent.AddListener(OnPickup);
this.pickup.OnInteractEndEvent.AddListener(OnDrop);
this.pickup.OnPickupUse.AddListener(OnUse);
this.network = Basis.SafeUtil.MakeNetworkable(this);
this.network.NetworkMessageReceived = OnNetworkMessage;
this.animator = GetComponent<Animator>();
}
public void OnNetworkMessage(ushort PlayerID, byte[] buffer, DeliveryMethod DeliveryMethod)
{
Debug.Log("Opticor: Received network message!, PlayerID: " + PlayerID + ", buffer length: " + buffer.Length + ", DeliveryMethod: " + DeliveryMethod);
if(buffer.Length < 1) return;
if(buffer[0] == 1)
{
//Debug.Log("Opticor: Start the Pew Pew! (networked)");
CheckShoot();
}
else if(buffer[0] == 2)
{
//Debug.Log("Opticor: Stop the Pew Pew! (networked)");
StopShoot();
}
}
private void OnPickup(BasisInput input)
{
//Debug.Log("Opticor: Picked up!");
}
private void OnDrop(BasisInput input)
{
//Debug.Log("Opticor: Dropped!");
}
void OnUse(BasisPickUpUseMode mode)
{
switch (mode)
{
case BasisPickUpUseMode.OnPickUpUseDown:
//Debug.Log("Opticor: Start the Pew Pew! (local)");
this.CheckShoot();
this.network.SendCustomNetworkEvent(new byte[] { 1 }, DeliveryMethod.ReliableSequenced);
break;
case BasisPickUpUseMode.OnPickUpUseUp:
//Debug.Log("Opticor: Stop the Pew Pew! (local)");
this.StopShoot();
this.network.SendCustomNetworkEvent(new byte[] { 2 }, DeliveryMethod.ReliableSequenced);
break;
}
}
private void CheckShoot()
{
animator.SetBool("GunTrigger", true);
if (this.animator.GetCurrentAnimatorStateInfo(0).IsName("Firing") || this.animator.GetCurrentAnimatorStateInfo(0).IsName("Misfire"))
return;
else
CheckIdleState();
void CheckIdleState()
{
if (this.animator.GetCurrentAnimatorStateInfo(0).IsName("Idle Overheated"))
{
this.animator.SetTrigger("GunMisfire");
audioSource.PlayOneShot(audioClipMisfire);
}
else
{
this.animator.SetTrigger("GunShoot");
audioSource.PlayOneShot(audioClipCharge);
}
}
}
private void StopShoot()
{
this.animator.SetBool("GunTrigger", false);
}
}The BasisNetworkShim is used to allow Cilbox scripts to have networking functionalities.


Edit on GitHub
Last updated on