First Attempt at making the arrow apply contact to objects

This commit is contained in:
2022-01-31 11:00:30 -06:00
parent 047f18e472
commit c6f1ecbbec
2 changed files with 57 additions and 11 deletions

View File

@@ -1,5 +1,6 @@
using MLAPI;
using MLAPI.Messaging;
using System.Collections;
using UnityEngine;
public class Arrow : NetworkBehaviour {
@@ -8,11 +9,29 @@ public class Arrow : NetworkBehaviour {
public float speed = 90f;
[SerializeField] private float bumpPower = 30;
//finds targed
public void Seek(UnityEngine.Vector3 _target) {
//can also do effects, speed on the bullet, damage amount, etc.
target = _target;
isLive = true;
// Start a co-routine to despawn the arrow if it doesn't hit anything
StartCoroutine(DespawnCountdown());
}
IEnumerator DespawnCountdown() {
int time = 15;
for (int i = time; i > 0; i--) {
yield return new WaitForSecondsRealtime(1f);
}
// Time's up - Despawn us | Only despawn once
if (IsHost) {
HitTargetServerRPC();
}
}
// Update is called once per frame
@@ -32,29 +51,41 @@ public class Arrow : NetworkBehaviour {
// 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);
}
}
// Is called whenever something collides with the bumper
void OnTriggerEnter(Collider objectHit) {
if (objectHit.transform.parent.gameObject.tag == "Player") {
//Checks if the other object is the player
PlayerMovement playerMovement = objectHit.GetComponent<PlayerMovement>();
float DirBumpX = playerMovement.driftVel.x * -1;//Inverts the Player Velocity x
float DirBumpY = .1f;
float DirBumpZ = playerMovement.driftVel.z * -1;//Inverts the Player Velocity z
Vector3 DirBump = new Vector3(DirBumpX, DirBumpY, DirBumpZ);//Creates a direction to launch the player
DirBump = Vector3.Normalize(DirBump);//Normalizes the vector to be used as a bump direction
playerMovement.GetHit(DirBump, bumpPower);
//
}
// If it hits anything else, we still want to despawn it
// Despawn the arrow
HitTargetServerRPC();
}
// Arrow has connected with something
[ServerRpc]
private void HitTargetServerRPC() {
isLive = false;
// Do the hit logic
// Despawn the arrow
this.gameObject.GetComponent<NetworkObject>().Despawn(true);
}
}