1
Fork 0
mirror of https://github.com/Steffo99/better-tee.git synced 2024-11-23 07:44:19 +00:00
better-tee/Assets/Packages/Mirror/Examples/AdditiveScenes/Scripts/ShootingTankBehaviour.cs

60 lines
1.7 KiB
C#
Raw Normal View History

2019-09-15 22:28:36 +00:00
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");
}
}
}
}