mirror of
https://github.com/Steffo99/better-tee.git
synced 2024-11-27 00:54:20 +00:00
60 lines
1.7 KiB
C#
60 lines
1.7 KiB
C#
|
using UnityEngine;
|
||
|
|
||
|
namespace Mirror.Examples.Additive
|
||
|
{
|
||
|
// This script demonstrates the NetworkAnimator and how to leverage
|
||
|
// the built-in observers system to track players.
|
||
|
// Note that all ProximityCheckers should be restricted to the Player layer.
|
||
|
public class ShootingTankBehaviour : NetworkBehaviour
|
||
|
{
|
||
|
[SyncVar]
|
||
|
public Quaternion rotation;
|
||
|
|
||
|
NetworkAnimator networkAnimator;
|
||
|
|
||
|
[ServerCallback]
|
||
|
void Start()
|
||
|
{
|
||
|
networkAnimator = GetComponent<NetworkAnimator>();
|
||
|
}
|
||
|
|
||
|
[Range(0, 1)]
|
||
|
public float turnSpeed = 0.1f;
|
||
|
|
||
|
void Update()
|
||
|
{
|
||
|
if (isServer && netIdentity.observers.Count > 0)
|
||
|
ShootNearestPlayer();
|
||
|
|
||
|
if (isClient)
|
||
|
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, turnSpeed);
|
||
|
}
|
||
|
|
||
|
[Server]
|
||
|
void ShootNearestPlayer()
|
||
|
{
|
||
|
GameObject target = null;
|
||
|
float distance = 100f;
|
||
|
|
||
|
foreach (NetworkConnection networkConnection in netIdentity.observers.Values)
|
||
|
{
|
||
|
GameObject tempTarget = networkConnection.playerController.gameObject;
|
||
|
float tempDistance = Vector3.Distance(tempTarget.transform.position, transform.position);
|
||
|
|
||
|
if (target == null || distance > tempDistance)
|
||
|
{
|
||
|
target = tempTarget;
|
||
|
distance = tempDistance;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (target != null)
|
||
|
{
|
||
|
transform.LookAt(target.transform.position + Vector3.down);
|
||
|
rotation = transform.rotation;
|
||
|
networkAnimator.SetTrigger("Fire");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|