This commit is contained in:
2023-11-28 11:41:03 +05:30
commit da3b6cf083
1281 changed files with 97466 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
// Useful for Text Meshes that should face the camera.
using UnityEngine;
namespace Mirror.Examples.Tanks
{
public class FaceCamera : MonoBehaviour
{
// LateUpdate so that all camera updates are finished.
void LateUpdate()
{
transform.forward = Camera.main.transform.forward;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: afa2d590c474413d9fc183551385ed85
timeCreated: 1632052051

View File

@@ -0,0 +1,35 @@
using UnityEngine;
namespace Mirror.Examples.Tanks
{
public class Projectile : NetworkBehaviour
{
public float destroyAfter = 2;
public Rigidbody rigidBody;
public float force = 1000;
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 co) => DestroySelf();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8f49b83f111a64bc7a5275af4f6f930b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,93 @@
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 = 4;
void Update()
{
// always update health bar.
// (SyncVar hook would only update on clients, not on server)
healthBar.text = new string('-', health);
// 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);
RaycastHit hit;
if (Physics.Raycast(ray, out 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,11 @@
fileFormatVersion: 2
guid: 7deadf756194d461e9140e42d651693b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: