0% found this document useful (0 votes)
24 views5 pages

2D Battle Royale Game Scripts

The document contains scripts for a multiplayer game using Unity and Netcode, including player controls, bullet mechanics, building structures, and a play zone that shrinks over time. Key functionalities include player movement, shooting, health management, and UI updates for health bars. The scripts also handle server-client interactions for actions like building and taking damage.

Uploaded by

marsloqmane87
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views5 pages

2D Battle Royale Game Scripts

The document contains scripts for a multiplayer game using Unity and Netcode, including player controls, bullet mechanics, building structures, and a play zone that shrinks over time. Key functionalities include player movement, shooting, health management, and UI updates for health bars. The scripts also handle server-client interactions for actions like building and taking damage.

Uploaded by

marsloqmane87
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

// PlayerController.

cs
using UnityEngine;
using Unity.Netcode;

public class PlayerController : NetworkBehaviour


{
public float moveSpeed = 5f;
public float maxHealth = 100f;
public float damage = 20f;
public float fireRate = 0.2f;
public GameObject bulletPrefab;
public Transform firePoint;
private NetworkVariable<float> health = new NetworkVariable<float>(100f);
private Rigidbody2D rb;
private Camera mainCamera;
private float nextFireTime;

public override void OnNetworkSpawn()


{
rb = GetComponent<Rigidbody2D>();
mainCamera = Camera.main;
health.Value = maxHealth;
if (IsOwner)
{
Cursor.lockState = CursorLockMode.Confined;
}
}

void Update()
{
if (!IsOwner) return;

float moveX = Input.GetAxisRaw("Horizontal");


float moveY = Input.GetAxisRaw("Vertical");
Vector2 moveDirection = new Vector2(moveX, moveY).normalized;
rb.velocity = moveDirection * moveSpeed;

Vector3 mousePos = mainCamera.ScreenToWorldPoint(Input.mousePosition);


mousePos.z = 0;
Vector2 lookDirection = (mousePos - transform.position).normalized;
if (lookDirection != Vector2.zero)
{
float angle = Mathf.Atan2(lookDirection.y, lookDirection.x) *
Mathf.Rad2Deg;
rb.rotation = angle;
}

if (Input.GetMouseButton(0) && Time.time >= nextFireTime)


{
ShootServerRpc(lookDirection);
nextFireTime = Time.time + fireRate;
}
}

[ServerRpc]
void ShootServerRpc(Vector2 direction)
{
GameObject bullet = Instantiate(bulletPrefab, firePoint.position,
Quaternion.identity);
bullet.GetComponent<Bullet>().Initialize(direction, damage);
bullet.GetComponent<NetworkObject>().Spawn();
}

[ServerRpc(RequireOwnership = false)]
public void TakeDamageServerRpc(float damage)
{
health.Value -= damage;
if (health.Value <= 0)
{
DieClientRpc();
}
}

[ClientRpc]
void DieClientRpc()
{
if (IsOwner)
{
GetComponent<NetworkObject>().Despawn();
}
}

public float GetHealth()


{
return health.Value;
}
}

// Bullet.cs
using UnityEngine;
using Unity.Netcode;

public class Bullet : MonoBehaviour


{
public float speed = 10f;
public float lifetime = 2f;
private float damage;
private Rigidbody2D rb;

void Awake()
{
rb = GetComponent<Rigidbody2D>();
Destroy(gameObject, lifetime);
}

public void Initialize(Vector2 direction, float bulletDamage)


{
damage = bulletDamage;
rb.velocity = direction * speed;
}

void OnTriggerEnter2D(Collider2D other)


{
if (other.CompareTag("Player"))
{
var player = other.GetComponent<PlayerController>();
if (player != null)
{
player.TakeDamageServerRpc(damage);
}
}
if (GetComponent<NetworkObject>().IsSpawned)
{
GetComponent<NetworkObject>().Despawn();
}
}
}

// PlayerBuilding.cs
using UnityEngine;
using Unity.Netcode;

public class PlayerBuilding : NetworkBehaviour


{
public GameObject wallPrefab;
public float buildRange = 2f;
public float buildCooldown = 0.5f;
private float nextBuildTime;
private Camera mainCamera;

public override void OnNetworkSpawn()


{
mainCamera = Camera.main;
}

void Update()
{
if (!IsOwner) return;

if (Input.GetKeyDown(KeyCode.B) && Time.time >= nextBuildTime)


{
BuildServerRpc();
nextBuildTime = Time.time + buildCooldown;
}
}

[ServerRpc]
void BuildServerRpc()
{
Vector3 mousePos = mainCamera.ScreenToWorldPoint(Input.mousePosition);
mousePos.z = 0;
Vector2 buildPos = (Vector2)transform.position + ((Vector2)(mousePos -
transform.position)).normalized * buildRange;
GameObject wall = Instantiate(wallPrefab, buildPos, Quaternion.identity);
wall.GetComponent<NetworkObject>().Spawn();
}
}

// PlayZone.cs
using UnityEngine;
using Unity.Netcode;

public class PlayZone : NetworkBehaviour


{
public float initialRadius = 50f;
public float shrinkSpeed = 0.5f;
public float damagePerSecond = 10f;
private NetworkVariable<float> currentRadius = new NetworkVariable<float>();
private Vector2 center;

public override void OnNetworkSpawn()


{
center = transform.position;
currentRadius.Value = initialRadius;
}

void Update()
{
if (!IsServer) return;

currentRadius.Value -= shrinkSpeed * Time.deltaTime;


currentRadius.Value = Mathf.Max(currentRadius.Value, 5f);
transform.localScale = new Vector3(currentRadius.Value * 2,
currentRadius.Value * 2, 1);

foreach (var player in FindObjectsOfType<PlayerController>())


{
float distance = Vector2.Distance(center, player.transform.position);
if (distance > currentRadius.Value)
{
player.TakeDamageServerRpc(damagePerSecond * Time.deltaTime);
}
}
}
}

// UIHealthBar.cs
using UnityEngine;
using UnityEngine.UI;
using Unity.Netcode;

public class UIHealthBar : NetworkBehaviour


{
public PlayerController player;
public Slider healthSlider;

public override void OnNetworkSpawn()


{
if (!IsOwner)
{
gameObject.SetActive(false);
return;
}
healthSlider.maxValue = player.maxHealth;
player.GetComponent<NetworkVariable<float>>().OnValueChanged +=
UpdateHealthBar;
UpdateHealthBar(0, player.GetHealth());
}

void UpdateHealthBar(float oldValue, float newValue)


{
healthSlider.value = newValue;
}
}

// NetworkManagerSetup.cs
using UnityEngine;
using Unity.Netcode;

public class NetworkManagerSetup : MonoBehaviour


{
public GameObject playZonePrefab;

void Start()
{
if (NetworkManager.Singleton.IsServer)
{
GameObject playZone = Instantiate(playZonePrefab, Vector3.zero,
Quaternion.identity);
playZone.GetComponent<NetworkObject>().Spawn();
}
}
}

You might also like