This commit is contained in:
2025-06-16 15:14:23 +02:00
commit 074e590073
3174 changed files with 428263 additions and 0 deletions

View File

@ -0,0 +1,94 @@
using System.Collections.Generic;
using UnityEngine;
using Mirror;
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(NetworkIdentity))]
[RequireComponent(typeof(NetworkTransformReliable))]
[DisallowMultipleComponent]
public class Box : NetworkBehaviour
{
[Header("Components")]
public Rigidbody rigidBody;
#region Unity Callbacks
/// <summary>
/// Add your validation code here after the base.OnValidate(); call.
/// </summary>
protected override void OnValidate()
{
if (Application.isPlaying) return;
base.OnValidate();
Reset();
}
void Reset()
{
rigidBody = GetComponent<Rigidbody>();
rigidBody.isKinematic = true;
}
#endregion
#region Start & Stop Callbacks
/// <summary>
/// This is invoked for NetworkBehaviour objects when they become active on the server.
/// <para>This could be triggered by NetworkServer.Listen() for objects in the scene, or by NetworkServer.Spawn() for objects that are dynamically created.</para>
/// <para>This will be called for objects on a "host" as well as for object on a dedicated server.</para>
/// </summary>
public override void OnStartServer()
{
rigidBody.isKinematic = false;
}
/// <summary>
/// Invoked on the server when the object is unspawned
/// <para>Useful for saving object data in persistent storage</para>
/// </summary>
public override void OnStopServer()
{
rigidBody.isKinematic = true;
}
/// <summary>
/// Called on every NetworkBehaviour when it is activated on a client.
/// <para>Objects on the host have this function called, as there is a local client on the host. The values of SyncVars on object are guaranteed to be initialized correctly with the latest state from the server when this function is called on the client.</para>
/// </summary>
public override void OnStartClient() { }
/// <summary>
/// This is invoked on clients when the server has caused this object to be destroyed.
/// <para>This can be used as a hook to invoke effects or do client specific cleanup.</para>
/// </summary>
public override void OnStopClient() { }
/// <summary>
/// Called when the local player object has been set up.
/// <para>This happens after OnStartClient(), as it is triggered by an ownership message from the server. This is an appropriate place to activate components or functionality that should only be active for the local player, such as cameras and input.</para>
/// </summary>
public override void OnStartLocalPlayer() { }
/// <summary>
/// Called when the local player object is being stopped.
/// <para>This happens before OnStopClient(), as it may be triggered by an ownership message from the server, or because the player object is being destroyed. This is an appropriate place to deactivate components or functionality that should only be active for the local player, such as cameras and input.</para>
/// </summary>
public override void OnStopLocalPlayer() {}
/// <summary>
/// This is invoked on behaviours that have authority, based on context and <see cref="NetworkIdentity.hasAuthority">NetworkIdentity.hasAuthority</see>.
/// <para>This is called after <see cref="OnStartServer">OnStartServer</see> and before <see cref="OnStartClient">OnStartClient.</see></para>
/// <para>When <see cref="NetworkIdentity.AssignClientAuthority">AssignClientAuthority</see> is called on the server, this will be called on the client that owns the object. When an object is spawned with <see cref="NetworkServer.Spawn">NetworkServer.Spawn</see> with a NetworkConnectionToClient parameter included, this will be called on the client that owns the object.</para>
/// </summary>
public override void OnStartAuthority() { }
/// <summary>
/// This is invoked on behaviours when authority is removed.
/// <para>When NetworkIdentity.RemoveClientAuthority is called on the server, this will be called on the client that owns the object.</para>
/// </summary>
public override void OnStopAuthority() { }
#endregion
}

View File

@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: bf9fc8899cb57ff4faff8849692b109f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129321
packageName: Mirror
packageVersion: 96.0.1
assetPath: Assets/Mirror/Examples/Tanks/Scripts/Box.cs
uploadId: 736421

View File

@ -0,0 +1,46 @@
using UnityEngine;
namespace Mirror.Examples.Tanks
{
public class Projectile : NetworkBehaviour
{
public float destroyAfter = 2f;
public Rigidbody rigidBody;
public float force = 1000f;
public override void OnStartServer()
{
Invoke(nameof(DestroySelf), destroyAfter);
}
// set velocity for server and client. this way we don't have to sync the
// position, because both the server and the client simulate it.
void Start()
{
rigidBody.AddForce(transform.forward * force);
}
// destroy for everyone on the server
[Server]
void DestroySelf()
{
NetworkServer.Destroy(gameObject);
}
// ServerCallback because we don't want a warning
// if OnTriggerEnter is called on the client
[ServerCallback]
void OnTriggerEnter(Collider other)
{
Debug.Log("Hit: " + other.name);
if (other.transform.parent.TryGetComponent(out Tank tank))
{
--tank.health;
if (tank.health == 0)
NetworkServer.RemovePlayerForConnection(tank.netIdentity.connectionToClient, RemovePlayerOptions.Destroy);
DestroySelf();
}
}
}
}

View File

@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 8f49b83f111a64bc7a5275af4f6f930b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129321
packageName: Mirror
packageVersion: 96.0.1
assetPath: Assets/Mirror/Examples/Tanks/Scripts/Projectile.cs
uploadId: 736421

View File

@ -0,0 +1,106 @@
using UnityEngine;
using UnityEngine.AI;
namespace Mirror.Examples.Tanks
{
public class Tank : NetworkBehaviour
{
[Header("Components")]
public NavMeshAgent agent;
public Animator animator;
public TextMesh healthBar;
public Transform turret;
[Header("Movement")]
public float rotationSpeed = 100;
[Header("Firing")]
public KeyCode shootKey = KeyCode.Space;
public GameObject projectilePrefab;
public Transform projectileMount;
[Header("Stats")]
[SyncVar] public int health = 5;
// naming for easier debugging
public override void OnStartClient()
{
name = $"Player[{netId}|{(isLocalPlayer ? "local" : "remote")}]";
}
public override void OnStartServer()
{
name = $"Player[{netId}|server]";
}
void Update()
{
// always update health bar.
// (SyncVar hook would only update on clients, not on server)
healthBar.text = new string('-', health);
// take input from focused window only
if(!Application.isFocused) return;
// movement for local player
if (isLocalPlayer)
{
// rotate
float horizontal = Input.GetAxis("Horizontal");
transform.Rotate(0, horizontal * rotationSpeed * Time.deltaTime, 0);
// move
float vertical = Input.GetAxis("Vertical");
Vector3 forward = transform.TransformDirection(Vector3.forward);
agent.velocity = forward * Mathf.Max(vertical, 0) * agent.speed;
animator.SetBool("Moving", agent.velocity != Vector3.zero);
// shoot
if (Input.GetKeyDown(shootKey))
{
CmdFire();
}
RotateTurret();
}
}
// this is called on the server
[Command]
void CmdFire()
{
GameObject projectile = Instantiate(projectilePrefab, projectileMount.position, projectileMount.rotation);
NetworkServer.Spawn(projectile);
RpcOnFire();
}
// this is called on the tank that fired for all observers
[ClientRpc]
void RpcOnFire()
{
animator.SetTrigger("Shoot");
}
//[ServerCallback]
//void OnTriggerEnter(Collider other)
//{
// if (other.GetComponent<Projectile>() != null)
// {
// --health;
// if (health == 0)
// NetworkServer.Destroy(gameObject);
// }
//}
void RotateTurret()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hit, 100))
{
Debug.DrawLine(ray.origin, hit.point);
Vector3 lookRotation = new Vector3(hit.point.x, turret.transform.position.y, hit.point.z);
turret.transform.LookAt(lookRotation);
}
}
}
}

View File

@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 7deadf756194d461e9140e42d651693b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129321
packageName: Mirror
packageVersion: 96.0.1
assetPath: Assets/Mirror/Examples/Tanks/Scripts/Tank.cs
uploadId: 736421