Assets/Deps/emotitron/NST Example/Scripts/Weapon.cs
/// <summary>
/// When a custom message is received, the OnCustomMsgRcvEvent is fired. Note that the rotations will only be correct if you have the NST set to update
/// rotations on events. If it is set to 'changes only' these rotations values will be zero.
/// </summary>
When a custom message is received, the OnCustomMsgRcvEvent is fired. Note that the rotations will only be correct if you have the NST set to update
rotations on events. If it is set to 'changes only' these rotations values will be zero.
When a custom message is received the OnCustomMsgRcvEvent is fired . Note that the rotations will only be correct if you have the NST set to update rotations on events . If it is set to ' changes only ' these rotations values will be zero .
private static void OnCustomMsgRcv(NetworkConnection ownerConn, byte[] bytearray, NetworkSyncTransform shooterNst, Vector3 pos, List<GenericX> positions, List<GenericX> rotations)
	{
		Weapon wpn = shooterNst.GetComponent<Weapon>();
		PlayerFireCustomMsg weaponFireMsg = bytearray.DeserializeToStruct<PlayerFireCustomMsg>();
		if ((WeaponType)weaponFireMsg.weaponId == WeaponType.Bullet)
		{
			if (!shooterNst.isLocalPlayer)
				wpn.FireBullet(pos, rotations[0], weaponFireMsg);
		}
		else if ((WeaponType)weaponFireMsg.weaponId == WeaponType.Hitscan)
		{
			uint confirmedHitmask = 0;
			if (!shooterNst.isLocalPlayer)
				wpn.DrawRay(pos, rotations[0]);
			if (NetworkServer.active)
			{
				for (int i = 0; i < 32; i++)
				{
					if (weaponFireMsg.hitmask.GetBitInMask(i) == false)
						continue;
					NetworkSyncTransform hitNst = NetworkSyncTransform.GetNstFromId((uint)i);
					bool hit = hitNst.TestHitscanAgainstRewind(ownerConn, new Ray(pos, (Quaternion)rotations[0] * Vector3.forward));
					if (hit)
						((int)hitNst.NstId).SetBitInMask(ref confirmedHitmask, true);
				}
				DebugText.Log("Rewind Confirmation Mask : \n" + confirmedHitmask.PrintBitMask(), true);
			}
		}
	}
private static void OnCustomMsgRcv ( NetworkConnection ownerConn , byte [ ] bytearray , NetworkSyncTransform shooterNst , Vector3 pos , List < GenericX > positions , List < GenericX > rotations ) { Weapon wpn = shooterNst . GetComponent < Weapon > ( ) ; PlayerFireCustomMsg weaponFireMsg = bytearray . DeserializeToStruct < PlayerFireCustomMsg > ( ) ; if ( ( WeaponType ) weaponFireMsg . weaponId == WeaponType . Bullet ) { if ( ! shooterNst . isLocalPlayer ) wpn . FireBullet ( pos , rotations [ 0 ] , weaponFireMsg ) ; } else if ( ( WeaponType ) weaponFireMsg . weaponId == WeaponType . Hitscan ) { uint confirmedHitmask = 0 ; if ( ! shooterNst . isLocalPlayer ) wpn . DrawRay ( pos , rotations [ 0 ] ) ; if ( NetworkServer . active ) { for ( int i = 0 ; i < 32 ; i ++ ) { if ( weaponFireMsg . hitmask . GetBitInMask ( i ) == false ) continue ; NetworkSyncTransform hitNst = NetworkSyncTransform . GetNstFromId ( ( uint ) i ) ; bool hit = hitNst . TestHitscanAgainstRewind ( ownerConn , new Ray ( pos , ( Quaternion ) rotations [ 0 ] * Vector3 . forward ) ) ; if ( hit ) ( ( int ) hitNst . NstId ) . SetBitInMask ( ref confirmedHitmask , true ) ; } DebugText . Log ( " \n " + confirmedHitmask . PrintBitMask ( ) , true ) ; } } }
When a custom message is received, the OnCustomMsgRcvEvent is fired.
When a custom message is received the OnCustomMsgRcvEvent is fired .
//For this example we fired locally already when the custom message was sent (with OnCustomMsgSend). Firing again here on the local player would cause repeat fire events. //Note however that code for the local player can be added here to sync the projectile with the server, by adding a projectileID to your custom events. // Hitscan arrive on server/other clients //DebugText.Log(weaponFireMsg.hitmask.PrintBitMask() + " RCV " + (WeaponType)weaponFireMsg.weaponId); // Draw the graphic if this isn't the local player // Server needs to test if this was a hit.
ownerConn
NetworkConnection
bytearray
byte[]
shooterNst
NetworkSyncTransform
pos
Vector3
positions
List<GenericX>
rotations
List<GenericX>