Initial Commit
This commit is contained in:
parent
53eb92e9af
commit
270ab7d11f
15341 changed files with 700234 additions and 0 deletions
57
Assets/Scripts/PlayerShooting.cs
Normal file
57
Assets/Scripts/PlayerShooting.cs
Normal file
|
@ -0,0 +1,57 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue