Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion Basic/2DSpaceShooter/Assets/Scenes/network.unity
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,7 @@ GameObject:
- component: {fileID: 872897446}
- component: {fileID: 872897444}
- component: {fileID: 872897445}
- component: {fileID: 872897447}
m_Layer: 0
m_Name: NetworkManager
m_TagString: Untagged
Expand Down Expand Up @@ -834,7 +835,7 @@ MonoBehaviour:
MaxReceiveEventsPerTickRate: 500
EventTickrate: 64
ClientConnectionBufferTimeout: 10
ConnectionApproval: 0
ConnectionApproval: 1
ConnectionData:
EnableTimeResync: 0
TimeResyncInterval: 30
Expand All @@ -856,6 +857,23 @@ MonoBehaviour:
type: {class: NullableBoolSerializable, ns: MLAPI.Configuration, asm: Unity.Multiplayer.MLAPI.Runtime}
data:
Value: 14735065859013011121
--- !u!114 &872897447
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 872897443}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: e23a6243f7715c44ea5d997e13ccbaee, type: 3}
m_Name:
m_EditorClassIdentifier:
m_SpawnMethod: 0
m_SpawnPositions:
- {x: 18, y: 12, z: 0}
- {x: 33, y: -32, z: 0}
- {x: 9, y: -25, z: 0}
--- !u!4 &872897449
Transform:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using MLAPI;
using UnityEngine;
using Random = UnityEngine.Random;

[RequireComponent(typeof(NetworkManager))]
public class RandomPositionPlayerSpawner: MonoBehaviour
{
NetworkManager m_NetworkManager;

int m_RoundRobinIndex = 0;

[SerializeField]
SpawnMethod m_SpawnMethod;

[SerializeField]
List<Vector3> m_SpawnPositions = new List<Vector3>() { Vector3.zero };

/// <summary>
/// Get a spawn position for a spawned object based on the spawn method.
/// </summary>
/// <returns>?The spawn position.</returns>
/// <exception cref="NotImplementedException"></exception>
public Vector3 GetNextSpawnPosition()
{
switch (m_SpawnMethod)
{
case SpawnMethod.Random:
var index = Random.Range(0, m_SpawnPositions.Count);
return m_SpawnPositions[index];
case SpawnMethod.RoundRobin:
m_RoundRobinIndex = (m_RoundRobinIndex + 1) % m_SpawnPositions.Count;
return m_SpawnPositions[m_RoundRobinIndex];
default:
throw new NotImplementedException();
}
}

private void Awake()
{
var networkManager = gameObject.GetComponent<NetworkManager>();
networkManager.ConnectionApprovalCallback += ConnectionApprovalWithRandomSpawnPos;
}

void ConnectionApprovalWithRandomSpawnPos(byte[] payload, ulong clientId, NetworkManager.ConnectionApprovedDelegate callback)
{
callback(true, null, true, GetNextSpawnPosition(), Quaternion.identity);
}
}

enum SpawnMethod
{
Random = 0,
RoundRobin = 1,
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Basic/2DSpaceShooter/Assets/Scripts/ShipControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void TakeDamage(int amount)

m_Deaths += 1;
Health.Value = 100;
transform.position = Vector3.zero;
transform.position = NetworkManager.GetComponent<RandomPositionPlayerSpawner>().GetNextSpawnPosition();
GetComponent<Rigidbody2D>().velocity = Vector3.zero;
GetComponent<Rigidbody2D>().angularVelocity = 0;
}
Expand Down