- Home /
Question by
magamestudios · Mar 14, 2021 at 09:23 PM ·
multiplayershootinggungameplay
How to make delay for shooting in Unity/photon 2
Here is my code:
using Photon.Pun; using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SingleShotGun : Gun { [SerializeField] Camera cam;
PhotonView PV;
public AudioSource m_shootingsound;
public GameObject bloodEffect;
float timeBetweenShots = 2;
void Start()
{
m_shootingsound = GetComponent<AudioSource>();
}
void Awake()
{
PV = GetComponent<PhotonView>();
}
public override void Use()
{
Shoot();
}
void Shoot()
{
Ray ray = cam.ViewportPointToRay(new Vector3(0.5f, 0.5f));
ray.origin = cam.transform.position;
if (Physics.Raycast(ray, out RaycastHit hit))
{
m_shootingsound.Play();
Debug.Log("Pow!");
hit.collider.gameObject.GetComponent<IDamageable>()?.TakeDamage(((GunInfo)itemInfo).damage);
PV.RPC("RPC_Shoot", RpcTarget.All, hit.point, hit.normal);
if (hit.transform.tag == "Player")
{
Instantiate(bloodEffect, hit.point, Quaternion.identity);
}
}
}
[PunRPC]
void RPC_Shoot(Vector3 hitPosition, Vector3 hitNormal)
{
Collider[] colliders = Physics.OverlapSphere(hitPosition, 0.3f);
if (colliders.Length != 0)
{
GameObject bulletImpactObj = Instantiate(bulletImpactPrefab, hitPosition + hitNormal * 0.001f, Quaternion.LookRotation(hitNormal, Vector3.up) * bulletImpactPrefab.transform.rotation);
Destroy(bulletImpactObj, 10f);
bulletImpactObj.transform.SetParent(colliders[0].transform);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Need help... Start Button no longer working!!!!! 0 Answers
Spawning a projectile over the server in UNET 0 Answers
Spawning objects in multiplayer game 1 Answer
Lag Compensated Projectile 0 Answers
How to add sound to gun shot script 4 Answers