- Home /
Question by
kotasugi2007 · Apr 18, 2020 at 12:34 PM ·
multiplayerphotonfirst person shooter
HELP ME ON PHOTON. UNITY 3D MULTIPLAYER FPS
Hello, I just started to create FPS multiplayer games by following a tutorial made by Welton King. Link: https://www.youtube.com/watch?v=MAAxL6Q_ElE&list=PL6PsTmPNvw0eZirNDjO8dL0Y6X0ZFGaMt I am stuck at #14. Somehow when I equip the weapon, it doesn't show on another person's screen. I have the same code as video. But somehow it seems like it is not working.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class Weapon : MonoBehaviourPunCallbacks
{
#region Variables
public Gun[] loadout;
public Transform WeaponParent;
public GameObject bulletholePrefab;
public LayerMask canBeShot;
private float currentCooldown;
private int currentIndex;
private GameObject currentWeapon;
#endregion
#region MonoBehaviour Callbacks
// Update is called once per frame
void Update()
{
if (!photonView.IsMine) return;
if (Input.GetKeyDown(KeyCode.Alpha1)) photonView.RPC("Equip", RpcTarget.All, 0);
if(currentWeapon != null)
{
Aim((Input.GetMouseButton(1)));
if(Input.GetMouseButtonDown(0) && currentCooldown <= 0)
{
photonView.RPC("Shoot", RpcTarget.All);
}
//Weapon position elasticity
currentWeapon.transform.localPosition = Vector3.Lerp(currentWeapon.transform.localPosition, Vector3.zero, Time.deltaTime * 4f);
//cooldown
if (currentCooldown > 0) currentCooldown -= Time.deltaTime;
}
}
#endregion
#region Private Methods
[PunRPC]
void Equip(int ind)
{
if (currentWeapon != null) Destroy(currentWeapon);
currentIndex = ind;
GameObject newEquipment = Instantiate (loadout[ind].prefab, WeaponParent.position, WeaponParent.rotation, WeaponParent) as GameObject;
newEquipment.transform.localPosition = Vector3.zero;
newEquipment.transform.localEulerAngles = Vector3.zero;
newEquipment.GetComponent<Sway>().enabled = photonView.IsMine;
currentWeapon = newEquipment;
}
void Aim(bool isAiming)
{
Transform anchor = currentWeapon.transform.Find("Anchor");
Transform state_ads = currentWeapon.transform.Find("States/ADS");
Transform state_hip = currentWeapon.transform.Find("States/Hip");
if (isAiming)
{
//aim
anchor.position = Vector3.Lerp(anchor.position, state_ads.position, Time.deltaTime * loadout[currentIndex].aimSpeed);
}
else
{
//hip
anchor.position = Vector3.Lerp(anchor.position, state_hip.position, Time.deltaTime * loadout[currentIndex].aimSpeed);
}
}
[PunRPC]
void Shoot()
{
Transform spawn = transform.Find("Cameras/Normal Camera");
//Blooom
Vector3 t_bloom = spawn.position + spawn.forward * 1000f;
t_bloom += Random.Range(-loadout[currentIndex].bloom, loadout[currentIndex].bloom) * spawn.up;
t_bloom += Random.Range(-loadout[currentIndex].bloom, loadout[currentIndex].bloom) * spawn.right;
t_bloom -= spawn.position;
t_bloom.Normalize();
//Cooldown
currentCooldown = loadout[currentIndex].fireRate;
//Raycast
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(spawn.position, t_bloom, out hit, 1000f, canBeShot))
{
GameObject newHole = Instantiate(bulletholePrefab, hit.point + hit.normal * 0.001f, Quaternion.identity) as GameObject;
newHole.transform.LookAt(hit.point + hit.normal);
Destroy(newHole, 5f);
if(photonView.IsMine)
{
//Shooting other player on network
if(hit.collider.gameObject.layer == 11)
{
//RPC Call ro Damage Player Gose Here
}
}
}
//gun fx
currentWeapon.transform.Rotate(-loadout[currentIndex].recoil, 0, 0);
currentWeapon.transform.position -= currentWeapon.transform.forward * loadout[currentIndex].kickback;
}
#endregion
}
↑ | | That is my weapon script. Sorry for putting the whole thing but I really need help with this script.
SOMEONE PLEASE HELP ME
screenshot-1.png
(470.4 kB)
screenshot-2.png
(307.1 kB)
Comment