Connection Establishment using IP Mirror

dev-ali
Ali Sharoz 2 months ago
parent 792f15f474
commit 119e95b787

@ -0,0 +1,16 @@
using UnityEngine;
using UnityEngine.UI;
public class GameUIManager : MonoBehaviour
{
public static GameUIManager Instance;
[Header("NOS UI")]
public Image boostBarFill;
public Button boostButton;
private void Awake()
{
Instance = this;
}
}

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7193227ad86d45a40864c925afb612e8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

@ -0,0 +1,74 @@
using Mirror;
using UnityEngine;
using UnityEngine.UI;
using System.Net;
using System.Net.Sockets;
public class MultiplayerMenuManager : MonoBehaviour
{
[Header("UI Panels")]
public GameObject hostPopup;
public GameObject joinPopup;
[Header("Host Popup Elements")]
public Text hostIPText;
[Header("Join Popup Elements")]
public InputField joinIPInput;
// === Called when HOST button is clicked ===
public void OnClick_Host()
{
string localIP = GetLocalIPAddress();
hostIPText.text = "Your IP: " + localIP;
hostPopup.SetActive(true); // Just show popup for now
}
public void OnClick_HostConfirm()
{
NetworkManager.singleton.StartHost(); // Actually starts the host now
}
// === Called when JOIN button is clicked ===
public void OnClick_Join()
{
joinPopup.SetActive(true);
}
// === Called when JOIN > START is clicked ===
public void OnClick_JoinConfirm()
{
string ip = joinIPInput.text.Trim();
if (!string.IsNullOrEmpty(ip))
{
NetworkManager.singleton.networkAddress = ip;
NetworkManager.singleton.StartClient();
}
}
// === Close popups ===
public void OnClick_CloseAllPopups()
{
hostPopup.SetActive(false);
joinPopup.SetActive(false);
}
// === Utility: Get local IP for LAN ===
private string GetLocalIPAddress()
{
string localIP = "Unavailable";
try
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIP = ip.ToString();
break;
}
}
}
catch { }
return localIP;
}
}

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5c7910c0c5d39bb47b6ce491363c38be
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

@ -7,7 +7,7 @@ PhysicMaterial:
m_PrefabInstance: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_Name: Friction m_Name: Friction
dynamicFriction: 0.06999985 dynamicFriction: 0.069981694
staticFriction: 1 staticFriction: 1
bounciness: 0.3 bounciness: 0.3
frictionCombine: 3 frictionCombine: 3

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 6cb797167da9e7f44925842f39f21c49
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: b7ef0b21caef7df4fbaf32ff73da212f
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 5906ecf9ac800ca43864c639d1817021
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

@ -1,6 +1,7 @@
using UnityEngine; using UnityEngine;
using Mirror;
public class CameraController : MonoBehaviour public class CameraController : NetworkBehaviour
{ {
[Header("Target")] [Header("Target")]
[SerializeField] Transform target; [SerializeField] Transform target;
@ -17,8 +18,20 @@ public class CameraController : MonoBehaviour
private Rigidbody targetRb; private Rigidbody targetRb;
private float currentDistance; private float currentDistance;
public Camera mainCam; public Camera mainCam;
private Camera cam;
private void Start() private void Start()
{ {
cam = GetComponent<Camera>();
// Check if we are the local player
if (!GetComponentInParent<NetworkIdentity>().isLocalPlayer)
{
if (cam != null) cam.enabled = false;
gameObject.SetActive(false); // disable the entire camera object
return;
}
Application.targetFrameRate = 120; Application.targetFrameRate = 120;
if (target == null) if (target == null)
{ {
@ -34,7 +47,7 @@ public class CameraController : MonoBehaviour
enabled = false; enabled = false;
return; return;
} }
mainCam=GetComponent<Camera>(); mainCam = GetComponent<Camera>();
currentDistance = baseDistance; currentDistance = baseDistance;
} }

@ -1,7 +1,8 @@
using Mirror;
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
public class NOSController : MonoBehaviour public class NOSController : NetworkBehaviour
{ {
[SerializeField] private float _currentNOS = 0f; [SerializeField] private float _currentNOS = 0f;
[SerializeField] private float _chargeSpeed = 1f; [SerializeField] private float _chargeSpeed = 1f;
@ -15,6 +16,16 @@ public class NOSController : MonoBehaviour
private MotionBlur _cameraMotionBlur; private MotionBlur _cameraMotionBlur;
public float NOSRatio => _currentNOS / 100f; public float NOSRatio => _currentNOS / 100f;
public Image _boostBarFill; public Image _boostBarFill;
//private NetworkIdentity identity;
public override void OnStartLocalPlayer()
{
Debug.Log("isLocalPlayer");
_boostBarFill = GameUIManager.Instance.boostBarFill;
GameUIManager.Instance.boostButton.onClick.RemoveAllListeners();
GameUIManager.Instance.boostButton.onClick.AddListener(ActivateNOS);
}
private void Awake() private void Awake()
{ {
@ -28,10 +39,12 @@ public class NOSController : MonoBehaviour
_boostAudio.volume = _audioVolume; _boostAudio.volume = _audioVolume;
} }
} }
private void Update() private void Update()
{ {
if (_vehicleHandler.AIControlled) return; if (!isLocalPlayer || _vehicleHandler.AIControlled)
return;
Debug.Log("NosController Local");
if (!_isBoosting) if (!_isBoosting)
{ {

@ -1,13 +1,13 @@
using UnityEngine; using UnityEngine;
using CnControls; using CnControls;
using Mirror;
public enum SurfaceDetection public enum SurfaceDetection
{ {
RayCast, RayCast,
SphereCast SphereCast
} }
public class VehicleController : MonoBehaviour public class VehicleController : NetworkBehaviour
{ {
[SerializeField] float currentSpeed = 0f; [SerializeField] float currentSpeed = 0f;
[SerializeField] float steeringAI = 0f; [SerializeField] float steeringAI = 0f;
@ -87,6 +87,7 @@ public class VehicleController : MonoBehaviour
public Vector3 LocalVelocity => velocityLocal; public Vector3 LocalVelocity => velocityLocal;
public Transform FollowTarget => cameraFollowTarget; public Transform FollowTarget => cameraFollowTarget;
[Range(0.3f, 1f)] public float steeringFactor = 1f; [Range(0.3f, 1f)] public float steeringFactor = 1f;
private NetworkIdentity identity;
private void Awake() private void Awake()
{ {
@ -95,6 +96,7 @@ public class VehicleController : MonoBehaviour
wheelRadius = sphereRigidbody.GetComponent<SphereCollider>().radius; wheelRadius = sphereRigidbody.GetComponent<SphereCollider>().radius;
nosController = GetComponent<NOSController>(); nosController = GetComponent<NOSController>();
maxGroundDistance = wheelRadius + 0.5f; maxGroundDistance = wheelRadius + 0.5f;
identity = GetComponentInParent<NetworkIdentity>();
InitializeVehicle(); InitializeVehicle();
} }
@ -115,7 +117,7 @@ public class VehicleController : MonoBehaviour
private void Update() private void Update()
{ {
if (!isRunning) return; if (identity == null || !identity.isLocalPlayer || !isRunning) return;
UpdateVisuals(); UpdateVisuals();
ProcessInputs(); ProcessInputs();
ApplyControls(); ApplyControls();
@ -186,7 +188,7 @@ public class VehicleController : MonoBehaviour
private void FixedUpdate() private void FixedUpdate()
{ {
if (!isRunning) return; if (identity == null || !identity.isLocalPlayer || !isRunning) return;
CheckGround(); CheckGround();
HandleMovement(); HandleMovement();
} }

@ -1,3 +1,4 @@
using Mirror;
using UnityEngine; using UnityEngine;
public class VehicleTracker : MonoBehaviour public class VehicleTracker : MonoBehaviour
@ -28,9 +29,10 @@ public class VehicleTracker : MonoBehaviour
public WaypointCircuit waypointsCircuit; public WaypointCircuit waypointsCircuit;
private float _offsetSwitchTimer; private float _offsetSwitchTimer;
private NetworkIdentity identity;
private void Start() private void Start()
{ {
identity = GetComponentInParent<NetworkIdentity>();
_linkedController = GetComponent<VehicleController>(); _linkedController = GetComponent<VehicleController>();
_targetTracker = new GameObject(name + " Tracker Target").transform; _targetTracker = new GameObject(name + " Tracker Target").transform;
_targetTracker.SetParent(transform); _targetTracker.SetParent(transform);
@ -39,10 +41,12 @@ public class VehicleTracker : MonoBehaviour
_distanceTraveled = 0f; _distanceTraveled = 0f;
_offsetTimerRandom = Random.Range(2.5f, 15f); _offsetTimerRandom = Random.Range(2.5f, 15f);
_offsetSwitchTimer = _offsetTimerRandom; _offsetSwitchTimer = _offsetTimerRandom;
waypointsCircuit=FindFirstObjectByType<WaypointCircuit>();
} }
private void Update() private void Update()
{ {
if (!identity.isLocalPlayer && !identity.isServer) return;
if (_linkedController.Running && _linkedController.AIControlled) if (_linkedController.Running && _linkedController.AIControlled)
{ {
_offsetSwitchTimer += Time.deltaTime; _offsetSwitchTimer += Time.deltaTime;
@ -71,12 +75,14 @@ public class VehicleTracker : MonoBehaviour
private void FixedUpdate() private void FixedUpdate()
{ {
if (!_linkedController.Running) if (_linkedController == null || !_linkedController.Running)
{ {
_isOffTrack = false; _isOffTrack = false;
return; return;
} }
// 👇 Restrict to local player or server
if (!identity.isLocalPlayer && !identity.isServer) return;
if (Physics.Raycast(transform.position - Vector3.down, -transform.up, out _surfaceHit, 10f)) if (Physics.Raycast(transform.position - Vector3.down, -transform.up, out _surfaceHit, 10f))
{ {
_isOffTrack = _surfaceHit.collider.CompareTag("Ground"); _isOffTrack = _surfaceHit.collider.CompareTag("Ground");

@ -7,6 +7,7 @@
"com.unity.recorder": "4.0.3", "com.unity.recorder": "4.0.3",
"com.unity.textmeshpro": "3.0.7", "com.unity.textmeshpro": "3.0.7",
"com.unity.timeline": "1.7.6", "com.unity.timeline": "1.7.6",
"com.unity.transport": "2.5.1",
"com.unity.ugui": "2.0.0", "com.unity.ugui": "2.0.0",
"com.unity.visualscripting": "1.9.4", "com.unity.visualscripting": "1.9.4",
"com.unity.modules.ai": "1.0.0", "com.unity.modules.ai": "1.0.0",

@ -1,5 +1,15 @@
{ {
"dependencies": { "dependencies": {
"com.unity.burst": {
"version": "1.8.18",
"depth": 1,
"source": "registry",
"dependencies": {
"com.unity.mathematics": "1.2.1",
"com.unity.modules.jsonserialize": "1.0.0"
},
"url": "https://packages.unity.com"
},
"com.unity.collab-proxy": { "com.unity.collab-proxy": {
"version": "2.7.1", "version": "2.7.1",
"depth": 0, "depth": 0,
@ -7,6 +17,18 @@
"dependencies": {}, "dependencies": {},
"url": "https://packages.unity.com" "url": "https://packages.unity.com"
}, },
"com.unity.collections": {
"version": "2.2.1",
"depth": 1,
"source": "registry",
"dependencies": {
"com.unity.burst": "1.8.8",
"com.unity.nuget.mono-cecil": "1.11.4",
"com.unity.modules.unityanalytics": "1.0.0",
"com.unity.test-framework.performance": "3.0.2"
},
"url": "https://packages.unity.com"
},
"com.unity.editorcoroutines": { "com.unity.editorcoroutines": {
"version": "1.0.0", "version": "1.0.0",
"depth": 1, "depth": 1,
@ -60,6 +82,20 @@
"dependencies": {}, "dependencies": {},
"url": "https://packages.unity.com" "url": "https://packages.unity.com"
}, },
"com.unity.mathematics": {
"version": "1.3.1",
"depth": 1,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.nuget.mono-cecil": {
"version": "1.11.4",
"depth": 2,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.nuget.newtonsoft-json": { "com.unity.nuget.newtonsoft-json": {
"version": "3.2.1", "version": "3.2.1",
"depth": 0, "depth": 0,
@ -110,6 +146,16 @@
}, },
"url": "https://packages.unity.com" "url": "https://packages.unity.com"
}, },
"com.unity.test-framework.performance": {
"version": "3.0.2",
"depth": 2,
"source": "registry",
"dependencies": {
"com.unity.test-framework": "1.1.31",
"com.unity.modules.jsonserialize": "1.0.0"
},
"url": "https://packages.unity.com"
},
"com.unity.testtools.codecoverage": { "com.unity.testtools.codecoverage": {
"version": "1.2.6", "version": "1.2.6",
"depth": 1, "depth": 1,
@ -141,6 +187,17 @@
}, },
"url": "https://packages.unity.com" "url": "https://packages.unity.com"
}, },
"com.unity.transport": {
"version": "2.5.1",
"depth": 0,
"source": "registry",
"dependencies": {
"com.unity.burst": "1.8.12",
"com.unity.collections": "2.2.1",
"com.unity.mathematics": "1.3.1"
},
"url": "https://packages.unity.com"
},
"com.unity.ugui": { "com.unity.ugui": {
"version": "2.0.0", "version": "2.0.0",
"depth": 0, "depth": 0,

Loading…
Cancel
Save