浏览代码
Players have cursors that they control that will collide with symbol objects. On a click, the host checks for proximity to a symbol object and "destroys" one if it was clicked on.
/main/staging/ngo_minigame_structure
Players have cursors that they control that will collide with symbol objects. On a click, the host checks for proximity to a symbol object and "destroys" one if it was clicked on.
/main/staging/ngo_minigame_structure
nathaniel.buck@unity3d.com
3 年前
当前提交
7e8eeb05
共有 9 个文件被更改,包括 318 次插入 和 6 次删除
-
13Assets/Prefabs/InGame/SymbolObject.prefab
-
15Assets/Scripts/Game/InGame/InGameRunner.cs
-
2Assets/Scripts/Game/InGame/SetupInGame.cs
-
157Assets/Prefabs/InGame/PlayerCursor.prefab
-
7Assets/Prefabs/InGame/PlayerCursor.prefab.meta
-
85Assets/Scripts/Game/InGame/PlayerCursor.cs
-
11Assets/Scripts/Game/InGame/PlayerCursor.cs.meta
-
23Assets/Scripts/Game/InGame/SymbolObject.cs
-
11Assets/Scripts/Game/InGame/SymbolObject.cs.meta
|
|||
%YAML 1.1 |
|||
%TAG !u! tag:unity3d.com,2011: |
|||
--- !u!1 &3227847727972158006 |
|||
GameObject: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
serializedVersion: 6 |
|||
m_Component: |
|||
- component: {fileID: 3227847727972158004} |
|||
- component: {fileID: -1321688216342888635} |
|||
- component: {fileID: 1724769857655924793} |
|||
- component: {fileID: 4403165428829500588} |
|||
- component: {fileID: 3227847727972158005} |
|||
- component: {fileID: -1476701697690489341} |
|||
m_Layer: 0 |
|||
m_Name: PlayerCursor |
|||
m_TagString: Untagged |
|||
m_Icon: {fileID: 0} |
|||
m_NavMeshLayer: 0 |
|||
m_StaticEditorFlags: 0 |
|||
m_IsActive: 1 |
|||
--- !u!4 &3227847727972158004 |
|||
Transform: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 3227847727972158006} |
|||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} |
|||
m_LocalPosition: {x: 0, y: 0, z: 0} |
|||
m_LocalScale: {x: 1, y: 1, z: 1} |
|||
m_Children: [] |
|||
m_Father: {fileID: 0} |
|||
m_RootOrder: 0 |
|||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} |
|||
--- !u!114 &-1321688216342888635 |
|||
MonoBehaviour: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 3227847727972158006} |
|||
m_Enabled: 1 |
|||
m_EditorHideFlags: 0 |
|||
m_Script: {fileID: 11500000, guid: d5a57f767e5e46a458fc5d3c628d0cbb, type: 3} |
|||
m_Name: |
|||
m_EditorClassIdentifier: |
|||
GlobalObjectIdHash: 951099334 |
|||
AlwaysReplicateAsRoot: 0 |
|||
DontDestroyWithOwner: 0 |
|||
AutoObjectParentSync: 1 |
|||
--- !u!114 &1724769857655924793 |
|||
MonoBehaviour: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 3227847727972158006} |
|||
m_Enabled: 1 |
|||
m_EditorHideFlags: 0 |
|||
m_Script: {fileID: 11500000, guid: e96cb6065543e43c4a752faaa1468eb1, type: 3} |
|||
m_Name: |
|||
m_EditorClassIdentifier: |
|||
SyncPositionX: 1 |
|||
SyncPositionY: 1 |
|||
SyncPositionZ: 1 |
|||
SyncRotAngleX: 0 |
|||
SyncRotAngleY: 0 |
|||
SyncRotAngleZ: 0 |
|||
SyncScaleX: 0 |
|||
SyncScaleY: 0 |
|||
SyncScaleZ: 0 |
|||
PositionThreshold: 0.001 |
|||
RotAngleThreshold: 0.01 |
|||
ScaleThreshold: 0.01 |
|||
InLocalSpace: 0 |
|||
Interpolate: 0 |
|||
CanCommitToTransform: 0 |
|||
--- !u!114 &4403165428829500588 |
|||
MonoBehaviour: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 3227847727972158006} |
|||
m_Enabled: 1 |
|||
m_EditorHideFlags: 0 |
|||
m_Script: {fileID: 11500000, guid: 1dff2bc874952454c9da192885efd32b, type: 3} |
|||
m_Name: |
|||
m_EditorClassIdentifier: |
|||
m_renderer: {fileID: 3227847727972158005} |
|||
--- !u!212 &3227847727972158005 |
|||
SpriteRenderer: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 3227847727972158006} |
|||
m_Enabled: 1 |
|||
m_CastShadows: 0 |
|||
m_ReceiveShadows: 0 |
|||
m_DynamicOccludee: 1 |
|||
m_MotionVectors: 1 |
|||
m_LightProbeUsage: 1 |
|||
m_ReflectionProbeUsage: 1 |
|||
m_RayTracingMode: 0 |
|||
m_RayTraceProcedural: 0 |
|||
m_RenderingLayerMask: 1 |
|||
m_RendererPriority: 0 |
|||
m_Materials: |
|||
- {fileID: 10754, guid: 0000000000000000f000000000000000, type: 0} |
|||
m_StaticBatchInfo: |
|||
firstSubMesh: 0 |
|||
subMeshCount: 0 |
|||
m_StaticBatchRoot: {fileID: 0} |
|||
m_ProbeAnchor: {fileID: 0} |
|||
m_LightProbeVolumeOverride: {fileID: 0} |
|||
m_ScaleInLightmap: 1 |
|||
m_ReceiveGI: 1 |
|||
m_PreserveUVs: 0 |
|||
m_IgnoreNormalsForChartDetection: 0 |
|||
m_ImportantGI: 0 |
|||
m_StitchLightmapSeams: 1 |
|||
m_SelectedEditorRenderState: 0 |
|||
m_MinimumChartSize: 4 |
|||
m_AutoUVMaxDistance: 0.5 |
|||
m_AutoUVMaxAngle: 89 |
|||
m_LightmapParameters: {fileID: 0} |
|||
m_SortingLayerID: 0 |
|||
m_SortingLayer: 0 |
|||
m_SortingOrder: 0 |
|||
m_Sprite: {fileID: 7482667652216324306, guid: 48e93eef0688c4a259cb0eddcd8661f7, type: 3} |
|||
m_Color: {r: 1, g: 1, b: 1, a: 1} |
|||
m_FlipX: 0 |
|||
m_FlipY: 0 |
|||
m_DrawMode: 0 |
|||
m_Size: {x: 1, y: 1} |
|||
m_AdaptiveModeThreshold: 0.5 |
|||
m_SpriteTileMode: 0 |
|||
m_WasSpriteAssigned: 1 |
|||
m_MaskInteraction: 0 |
|||
m_SpriteSortPoint: 0 |
|||
--- !u!65 &-1476701697690489341 |
|||
BoxCollider: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 3227847727972158006} |
|||
m_Material: {fileID: 0} |
|||
m_IsTrigger: 1 |
|||
m_Enabled: 1 |
|||
serializedVersion: 2 |
|||
m_Size: {x: 1, y: 1, z: 0.2} |
|||
m_Center: {x: 0, y: 0, z: 0} |
|
|||
fileFormatVersion: 2 |
|||
guid: 905594b4ee5bb864a84af916cc445d1b |
|||
PrefabImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System.Collections.Generic; |
|||
using Unity.Netcode; |
|||
using UnityEngine; |
|||
|
|||
namespace LobbyRelaySample.inGame |
|||
{ |
|||
/// <summary>
|
|||
/// Each player's cursor needs to be controlled by them and visible to the other players.
|
|||
/// </summary>
|
|||
public class PlayerCursor : NetworkBehaviour |
|||
{ |
|||
private Camera m_mainCamera; |
|||
private NetworkVariable<Vector3> m_position = new NetworkVariable<Vector3>(NetworkVariableReadPermission.Everyone, Vector3.zero); |
|||
|
|||
// The host is responsible for determining if a player has successfully selected a symbol object, since collisions should be handled serverside.
|
|||
private List<SymbolObject> m_currentlyCollidingSymbols; |
|||
|
|||
// We can't pass object references as RPC calls by default, and we don't have a different convenient way to, I think,
|
|||
// get the object spawned on the server to assign some member on the client, so instead let's retrieve dynamically what we need.
|
|||
// I guess I'd just have a "singleton" to hold the references?
|
|||
public override void OnNetworkSpawn() |
|||
{ |
|||
m_mainCamera = GameObject.Find("InGameCamera").GetComponent<Camera>(); |
|||
if (IsHost) |
|||
m_currentlyCollidingSymbols = new List<SymbolObject>(); |
|||
} |
|||
|
|||
// Don't love having the input here, but it doesn't need to be anywhere else.
|
|||
private bool IsSelectInputHit() |
|||
{ |
|||
return Input.GetMouseButtonDown(0); |
|||
} |
|||
|
|||
public void Update() // TODO: FixedUpdate?
|
|||
{ |
|||
transform.position = m_position.Value; |
|||
if (m_mainCamera == null || !IsOwner) |
|||
return; |
|||
|
|||
Vector3 targetPos = (Vector2)m_mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -m_mainCamera.transform.position.z)); |
|||
SetPosition_ServerRpc(targetPos); // Client can't set a network variable value.
|
|||
if (IsSelectInputHit()) |
|||
SendInput_ServerRpc(); |
|||
} |
|||
|
|||
[ServerRpc] // Leave RequireOwnership = true for these so that only the player whose cursor this is can make updates.
|
|||
private void SetPosition_ServerRpc(Vector3 position) |
|||
{ |
|||
m_position.Value = position; |
|||
} |
|||
|
|||
[ServerRpc] |
|||
private void SendInput_ServerRpc() |
|||
{ |
|||
if (m_currentlyCollidingSymbols.Count > 0) |
|||
{ |
|||
SymbolObject symbol = m_currentlyCollidingSymbols[0]; |
|||
m_currentlyCollidingSymbols.RemoveAt(0); |
|||
symbol.OnSelect(); |
|||
} |
|||
} |
|||
|
|||
public void OnTriggerEnter(Collider other) |
|||
{ |
|||
if (!IsHost) |
|||
return; |
|||
SymbolObject symbol = other.GetComponent<SymbolObject>(); |
|||
if (symbol == null) |
|||
return; |
|||
if (!m_currentlyCollidingSymbols.Contains(symbol)) |
|||
m_currentlyCollidingSymbols.Add(symbol); |
|||
} |
|||
|
|||
public void OnTriggerExit(Collider other) |
|||
{ |
|||
if (!IsHost) |
|||
return; |
|||
SymbolObject symbol = other.GetComponent<SymbolObject>(); |
|||
if (symbol == null) |
|||
return; |
|||
if (m_currentlyCollidingSymbols.Contains(symbol)) |
|||
m_currentlyCollidingSymbols.Remove(symbol); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 1dff2bc874952454c9da192885efd32b |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using Unity.Netcode; |
|||
using UnityEngine; |
|||
|
|||
namespace LobbyRelaySample.inGame |
|||
{ |
|||
public class SymbolObject : NetworkBehaviour |
|||
{ |
|||
public void OnSelect() |
|||
{ |
|||
Destroy_ServerRpc(); |
|||
} |
|||
|
|||
[ServerRpc(RequireOwnership = false)] |
|||
private void Destroy_ServerRpc() |
|||
{ |
|||
// Actually destroying the symbol objects can cause garbage collection and other delays that might lead to desyncs.
|
|||
// Instead, just deactivate the object, and it will be cleaned up once the NetworkManager is destroyed.
|
|||
// (If object pooling, this is where to instead return it to the pool.)
|
|||
this.transform.localPosition = Vector3.down * 500; |
|||
// TODO: Visually disappear immediately.
|
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 63adacb71a52dcb41be1952af26573d8 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue