Finished Networking the Boulder

This commit is contained in:
2021-12-10 22:30:40 -06:00
parent b74c71004d
commit 2c2a13ff84
7 changed files with 254 additions and 62 deletions

View File

@@ -1,8 +1,10 @@
using MLAPI;
using MLAPI.Messaging;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BoulderSpawn : MonoBehaviour
public class BoulderSpawn : NetworkBehaviour
{
[SerializeField] private float RespawnTimer = 10.0f;
private float currentTime = 0.0f;
@@ -13,22 +15,32 @@ public class BoulderSpawn : MonoBehaviour
[SerializeField] private Transform BoulderPrefab;
private GameObject boulderInScene;
// Start is called before the first frame update
void Start()
{
if (IsHost)
{
SpawnBoulderServerRPC();
}
}
[ServerRpc(RequireOwnership = false)]
private void SpawnBoulderServerRPC() {
boulderInScene = Instantiate(BoulderPrefab, SpawnLocation, Quaternion.identity).gameObject;
boulderInScene.GetComponent<Rigidbody>().AddForce(spawnForce);
boulderInScene.GetComponent<NetworkObject>().Spawn(null, true);
boulderInScene.GetComponent<Boulder>().StartCountdown((int)RespawnTimer, spawnForce);
}
private void FixedUpdate()
{
if (!IsHost) { return; }
currentTime += Time.fixedDeltaTime;
if (currentTime >= RespawnTimer)
{
currentTime = 0.0f;
Destroy(boulderInScene);
boulderInScene = Instantiate(BoulderPrefab, SpawnLocation, Quaternion.identity).gameObject;
boulderInScene.GetComponent<Rigidbody>().AddForce(spawnForce);
SpawnBoulderServerRPC();
}
}

View File

@@ -1,8 +1,10 @@
using MLAPI;
using MLAPI.Messaging;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Boulder : MonoBehaviour
public class Boulder : NetworkBehaviour
{
private Rigidbody boulder;
@@ -23,15 +25,29 @@ public class Boulder : MonoBehaviour
}
}
//Coppied from player for easy Boulder Testing/Demonstration
void FixedUpdate() {
Respawn();
public void StartCountdown(int time, Vector3 spawnForce)
{
StartCoroutine(DespawnCounter(time));
AddForceServerRPC(spawnForce);
}
private void Respawn()
{
if (transform.position.y < -1) {
transform.position = new Vector3(74.67f, 34.68f, 7.15f);
[ServerRpc]
private void AddForceServerRPC(Vector3 force) {
this.gameObject.GetComponent<Rigidbody>().AddForce(force);
}
IEnumerator DespawnCounter(int time) {
for (int i = time; i > 0; i--) {
Debug.LogError(i);
yield return new WaitForSecondsRealtime(1f);
}
// Time's up - Despawn us
DespawnBoulderServerRPC();
}
[ServerRpc(RequireOwnership = false)]
private void DespawnBoulderServerRPC() {
this.gameObject.GetComponent<NetworkObject>().Despawn(true);
}
}