1
Fork 0
mirror of https://github.com/Steffo99/better-tee.git synced 2024-11-27 00:54:20 +00:00
better-tee/Assets/Packages/Mirror/Examples/AdditiveScenes/Scripts/ShootingTankBehaviour.cs
2019-09-16 00:28:36 +02:00

59 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");
}
}
}
}