evershade/Assets/Scripts/Controllers/PlayerSpawnObject.cs

44 lines
1.1 KiB
C#

using FishNet.Object;
using UnityEngine;
public class PlayerSpawnObject : NetworkBehaviour
{
public GameObject objToSpawn;
[HideInInspector] public GameObject spawnedObj;
public override void OnStartClient()
{
base.OnStartClient();
if (!IsOwner)
GetComponent<PlayerSpawnObject>().enabled = false;
}
public void Update()
{
if(spawnedObj == null && Input.GetKeyDown(KeyCode.Space))
{
SpawnObject(objToSpawn, transform, this);
}
}
[ServerRpc]
public void SpawnObject(GameObject obj, Transform player, PlayerSpawnObject script)
{
GameObject spawned = Instantiate(obj, player.position + player.forward, Quaternion.identity);
ServerManager.Spawn(spawned);
}
[ObserversRpc]
public void SpawnObjectClient(GameObject obj, PlayerSpawnObject script)
{
script.spawnedObj = obj;
}
[ServerRpc(RequireOwnership = false)]
public void DestroyObjectServerRpc(GameObject obj)
{
ServerManager.Despawn(obj);
}
}