TheKingsRace/Assets/Scripts/Enemy scripts/Arrow.cs
Julia Butenhoff 4d43e3c832 Shooting work started. Needs Hit Detection Revisions
Waiting on Karl to fix major red flags before I continue this
2022-01-28 21:36:05 -06:00

61 lines
1.5 KiB
C#

using MLAPI;
using MLAPI.Messaging;
using UnityEngine;
public class Arrow : NetworkBehaviour {
private Vector3 target;
private bool isLive = false;
public float speed = 90f;
//finds targed
public void Seek(UnityEngine.Vector3 _target) {
//can also do effects, speed on the bullet, damage amount, etc.
target = _target;
isLive = true;
}
// Update is called once per frame
void Update()
{
// Only move the arrow on the host
if (!IsHost) { return; }
// This keeps the static arrows from moving without a target
if (target == null) { return; }
// The arrow is allowed to move towards a target
if (isLive) {
// Find the direction to fire in
Vector3 dir = target - transform.position;
// Calculate distance
float distanceThisFrame = speed * Time.deltaTime;
// Checking from current distance to current target
if (dir.magnitude <= distanceThisFrame) {
// NOT SURE IF THIS CALC ^ WORKS AS INTENDED
HitTargetServerRPC();
return;
}
//move in the worldspace
transform.Translate(dir.normalized * distanceThisFrame, Space.World);
}
}
// Arrow has connected with something
[ServerRpc]
private void HitTargetServerRPC() {
isLive = false;
// Do the hit logic
// Despawn the arrow
this.gameObject.GetComponent<NetworkObject>().Despawn(true);
}
}