polygone/Assets/Scripts/PlayerShooting.cs
2021-08-02 05:44:37 -04:00

57 lines
1.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAPI;
using MLAPI.NetworkVariable;
using MLAPI.Messaging;
public class PlayerShooting : NetworkBehaviour
{
public ParticleSystem bulletParticles;
private ParticleSystem.EmissionModule em;
NetworkVariableBool isShooting = new NetworkVariableBool(new NetworkVariableSettings { WritePermission = NetworkVariablePermission.OwnerOnly }, false);
float fireRate = 10f;
float shootTimer = 0f;
// Start is called before the first frame update
void Start()
{
em = bulletParticles.emission;
}
// Update is called once per frame
void Update()
{
if (IsLocalPlayer)
{
isShooting.Value = Input.GetMouseButton(0);
shootTimer += Time.deltaTime;
if (isShooting.Value && shootTimer >= 1f/fireRate)
{
shootTimer = 0;
FireServerRpc();
}
}
em.rateOverTime = isShooting.Value ? fireRate : 0f;
}
[ServerRpc]
void FireServerRpc()
{
Ray ray = new Ray(bulletParticles.transform.position, bulletParticles.transform.forward);
if (Physics.Raycast(ray, out RaycastHit hit, 100f))
{
var player = hit.collider.GetComponent<PlayerHealth>();
if (player != null)
{
player.TakeDamage(10f);
}
}
}
}