This commit is contained in:
2025-06-16 15:14:23 +02:00
committed by devbeni
parent 60fe4620ff
commit 4ff561284f
3174 changed files with 428263 additions and 0 deletions

View File

@ -0,0 +1,69 @@
using UnityEngine;
namespace Mirror.Examples.Pong
{
public class Ball : NetworkBehaviour
{
public float speed = 30;
public Rigidbody2D rigidbody2d;
public override void OnStartServer()
{
base.OnStartServer();
// only simulate ball physics on server
rigidbody2d.simulated = true;
// Serve the ball from left player
#if UNITY_6000_0_OR_NEWER
rigidbody2d.linearVelocity = Vector2.right * speed;
#else
rigidbody2d.velocity = Vector2.right * speed;
#endif
}
float HitFactor(Vector2 ballPos, Vector2 racketPos, float racketHeight)
{
// ascii art:
// || 1 <- at the top of the racket
// ||
// || 0 <- at the middle of the racket
// ||
// || -1 <- at the bottom of the racket
return (ballPos.y - racketPos.y) / racketHeight;
}
// only call this on server
[ServerCallback]
void OnCollisionEnter2D(Collision2D col)
{
// Note: 'col' holds the collision information. If the
// Ball collided with a racket, then:
// col.gameObject is the racket
// col.transform.position is the racket's position
// col.collider is the racket's collider
// did we hit a racket? then we need to calculate the hit factor
if (col.transform.GetComponent<Player>())
{
// Calculate y direction via hit Factor
float y = HitFactor(transform.position,
col.transform.position,
col.collider.bounds.size.y);
// Calculate x direction via opposite collision
float x = col.relativeVelocity.x > 0 ? 1 : -1;
// Calculate direction, make length=1 via .normalized
Vector2 dir = new Vector2(x, y).normalized;
// Set Velocity with dir * speed
#if UNITY_6000_0_OR_NEWER
rigidbody2d.linearVelocity = dir * speed;
#else
rigidbody2d.velocity = dir * speed;
#endif
}
}
}
}

View File

@ -0,0 +1,19 @@
fileFormatVersion: 2
guid: 38b5c2f743cd8034a8beeebf277c92c1
timeCreated: 1426602353
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129321
packageName: Mirror
packageVersion: 96.0.1
assetPath: Assets/Mirror/Examples/Pong/Scripts/Ball.cs
uploadId: 736421

View File

@ -0,0 +1,45 @@
using UnityEngine;
/*
Documentation: https://mirror-networking.gitbook.io/docs/components/network-manager
API Reference: https://mirror-networking.com/docs/api/Mirror.NetworkManager.html
*/
namespace Mirror.Examples.Pong
{
// Custom NetworkManager that simply assigns the correct racket positions when
// spawning players. The built in RoundRobin spawn method wouldn't work after
// someone reconnects (both players would be on the same side).
[AddComponentMenu("")]
public class NetworkManagerPong : NetworkManager
{
public Transform leftRacketSpawn;
public Transform rightRacketSpawn;
GameObject ball;
public override void OnServerAddPlayer(NetworkConnectionToClient conn)
{
// add player at correct spawn position
Transform start = numPlayers == 0 ? leftRacketSpawn : rightRacketSpawn;
GameObject player = Instantiate(playerPrefab, start.position, start.rotation);
NetworkServer.AddPlayerForConnection(conn, player);
// spawn ball if two players
if (numPlayers == 2)
{
ball = Instantiate(spawnPrefabs.Find(prefab => prefab.name == "Ball"));
NetworkServer.Spawn(ball);
}
}
public override void OnServerDisconnect(NetworkConnectionToClient conn)
{
// destroy ball
if (ball != null)
NetworkServer.Destroy(ball);
// call base functionality (actually destroys the player)
base.OnServerDisconnect(conn);
}
}
}

View File

@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 0aa3018bb284840d6a6d0acee29ab098
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129321
packageName: Mirror
packageVersion: 96.0.1
assetPath: Assets/Mirror/Examples/Pong/Scripts/NetworkManagerPong.cs
uploadId: 736421

View File

@ -0,0 +1,23 @@
using UnityEngine;
namespace Mirror.Examples.Pong
{
public class Player : NetworkBehaviour
{
public float speed = 30;
public Rigidbody2D rigidbody2d;
// need to use FixedUpdate for rigidbody
void FixedUpdate()
{
// only let the local player control the racket.
// don't control other player's rackets
if (isLocalPlayer)
#if UNITY_6000_0_OR_NEWER
rigidbody2d.linearVelocity = new Vector2(0, Input.GetAxisRaw("Vertical")) * speed * Time.fixedDeltaTime;
#else
rigidbody2d.velocity = new Vector2(0, Input.GetAxisRaw("Vertical")) * speed * Time.fixedDeltaTime;
#endif
}
}
}

View File

@ -0,0 +1,19 @@
fileFormatVersion: 2
guid: 0748c7eda22b19845b9ce0e4d23d1021
timeCreated: 1426597826
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129321
packageName: Mirror
packageVersion: 96.0.1
assetPath: Assets/Mirror/Examples/Pong/Scripts/Player.cs
uploadId: 736421