I am developing a game called cyberlight because my friend wanted to see a multiplayer game come out of me for once. i got pretty far, and i got stuck on picking up weapons. i only need a few kinds so thats why theyre hard coded. after i tested it without networking, it was fine. if i added networking however, it just wouldnt work. i cant pick up weapons and the weapons wouldnt even sync up as in positionally. here’s the grab code, aka the camera code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class camera : NetworkBehaviour
{
public LayerMask weapon, player1;
GameObject inHand;
public camera cam1;
public float ZOOM, distance;
public int ak47Damage, pistolDamage, shotgunDamage, awpDamage, rocketDamage, healthcoinHealth;
public int ak47Ammo, pistolAmmo, shotgunAmmo, awpAmmo, rocketAmmo;
int currentak47Ammo, currentpistolAmmo = 5, currentshotgunAmmo, currentawpAmmo, currentrocketAmmo;
int currentak47Mags = 5, currentpistolMags = 5, currentshotgunMags, currentawpMags, currentrocketMags;
public int ak47ReloadTime, pistolReloadTime, shotgunReloadTime, awpReloadTime, rocketReloadTime;
public GameObject player, GunPosition, recoilpos, aimpos, spark;
bool hasitem;
float firetimer;
ParticleSystem ps;
bool isaiming;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
hasitem = false;
gameObject.GetComponent<Camera>().fieldOfView = 60;
}
// Update is called once per frame
void Update()
{
if (!isLocalPlayer)
{
// exit from update if this is not the local player
return;
}
if (Input.GetKeyDown(KeyCode.E))
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, 3.5f, weapon))//raycast from the center of the screen
{
Debug.Log("hit!");
if (hasitem)
{// if i already have an item, drop it, then grab the new one
inHand.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
inHand.GetComponent<Rigidbody>().isKinematic = false;
inHand.GetComponent<Rigidbody>().detectCollisions = true;
inHand.GetComponent<Rigidbody>().velocity = transform.forward * 5f;
inHand.GetComponent<Rigidbody>().angularVelocity = new Vector3(13, 5, 20);
inHand = hit.collider.gameObject;
}
else
{//otherwise, grab the new one
inHand = hit.collider.gameObject;
inHand.GetComponent<Rigidbody>().isKinematic = true;
hasitem = true;
}
}
else if (hasitem)
{//if i did not hit, my guy still pressed E so i guess ill drop the current weapon
inHand.GetComponent<Rigidbody>().velocity = transform.forward * 5f;
inHand.GetComponent<Rigidbody>().isKinematic = false;
inHand.transform.parent = null;
inHand.GetComponent<Rigidbody>().detectCollisions = true;
inHand.GetComponent<Rigidbody>().angularVelocity = new Vector3(0.1f, 0.1f, 0.1f);
inHand.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
inHand.GetComponent<Rigidbody>().velocity = transform.forward * 5f;
inHand.GetComponent<Rigidbody>().angularVelocity = new Vector3(13, 5, 20);
inHand = null;
gameObject.GetComponent<Camera>().fieldOfView = 60;
hasitem = false;
}
}
if (hasitem)
{//if i have a weapon, move it to the hand position.
inHand.GetComponent<Rigidbody>().velocity = Vector3.zero;
inHand.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
inHand.GetComponent<Rigidbody>().isKinematic = true;
inHand.GetComponent<Rigidbody>().detectCollisions = false;
if (!isaiming)
{ // if i am not aiming, move it to the default hand position
inHand.transform.position = Vector3.Slerp(inHand.transform.position, GunPosition.transform.position, Mathf.Clamp(25f * Time.deltaTime, 0, 0.7f));
gameObject.GetComponent<Camera>().fieldOfView = Mathf.Lerp(gameObject.GetComponent<Camera>().fieldOfView, 60, 0.1f);
inHand.transform.rotation = Quaternion.Slerp(inHand.transform.rotation, GunPosition.transform.rotation, Time.deltaTime * 10);
}
else
{// if i AM aiming, move it to the aim position.
inHand.transform.rotation = Quaternion.Slerp(inHand.transform.rotation, GunPosition.transform.rotation, Time.deltaTime * 10);
gameObject.GetComponent<Camera>().fieldOfView = Mathf.Lerp(gameObject.GetComponent<Camera>().fieldOfView, ZOOM, 0.1f);
}
ps = inHand.GetComponent<ParticleSystem>();
}
transform.localRotation = Quaternion.Lerp(transform.localRotation, Quaternion.Euler(0, 0, 0), 10f * Time.deltaTime);
// |-----------------------------------------------------------------------|
// |-----------------------------WEAPONSYSTEM------------------------------|
// |-----------------------------------------------------------------------|
RaycastHit bullet;
if (!(firetimer < 1))
firetimer -= 1 * Time.deltaTime;
if (hasitem)
{
if (inHand.tag == "pistol")
if (Input.GetButtonDown("Fire1") && firetimer < 1)
{
if (currentpistolAmmo != 0)
{
ps.Play();
Debug.Log("FIRE");
inHand.transform.position = recoilpos.transform.position;
inHand.transform.rotation = recoilpos.transform.rotation;
firetimer = 0.25f;
transform.localRotation = Quaternion.Euler(transform.localRotation.x - 0.5f, transform.localRotation.y, transform.localRotation.z);
if (Physics.Raycast(aimpos.transform.position, transform.forward, out bullet, 9999999f))
{
if (bullet.collider.gameObject.layer == player1)
{
bullet.collider.gameObject.GetComponent<AI_bot>().Hp -= pistolDamage;
}
else
{
Instantiate(spark, bullet.point, Quaternion.Euler(Vector3.zero));
}
currentpistolAmmo -= 1;
}
} else
{
Reload();
}
}
if (inHand.tag == "ak47")
if (Input.GetButton("Fire1") && firetimer < 1)
{
ps.Play();
Debug.Log("FIRE");
inHand.transform.position = recoilpos.transform.position;
inHand.transform.rotation = recoilpos.transform.rotation;
firetimer = 1.125f;
transform.localRotation = Quaternion.Euler(transform.localRotation.x + Random.Range(-2f, 2f), transform.localRotation.y + Random.Range(-2f, 2f), transform.localRotation.z);
if (Physics.Raycast(aimpos.transform.position, transform.forward, out bullet, 9999999f))
{
if (bullet.collider.gameObject.layer == player1)
{
bullet.collider.gameObject.GetComponent<AI_bot>().Hp -= ak47Damage;
}
else
{
Instantiate(spark, bullet.point, Quaternion.Euler(Vector3.zero));
}
}
}
if (inHand.tag == "rpg")
if (Input.GetButton("Fire1") && firetimer < 1)
{
ps.Play();
inHand.GetComponent<Rlauncher>().Fire = true;
inHand.gameObject.transform.gameObject.transform.gameObject.transform.localPosition = inHand.transform.gameObject.transform.gameObject.transform.position;
Debug.Log("FIRE");
inHand.transform.position = recoilpos.transform.position;
inHand.transform.rotation = recoilpos.transform.rotation;
firetimer = 4;
transform.localRotation = Quaternion.Euler(transform.localRotation.x - 2f, transform.localRotation.y, transform.localRotation.z);
}
if (inHand.tag == "mg")
if (Input.GetButtonDown("Fire1") && firetimer < 1)
{
if (Physics.Raycast(aimpos.transform.position, transform.forward, out bullet, 9999999f))
{
if (bullet.collider.gameObject.layer == player1)
{
bullet.collider.gameObject.GetComponent<AI_bot>().Hp -= shotgunDamage;
}
else
{
Instantiate(spark, bullet.point, Quaternion.Euler(Vector3.zero));
}
}
ps.Play();
Debug.Log("FIRE");
inHand.transform.position = recoilpos.transform.position;
inHand.transform.rotation = recoilpos.transform.rotation;
firetimer = 1.6f;
transform.localRotation = Quaternion.Euler(transform.localRotation.x - 17f, transform.localRotation.y, transform.localRotation.z);
}
if (inHand.tag == "sniper")
{
if (Input.GetButtonDown("Fire2"))
{
isaiming = true;
player.GetComponent<player>().sensMultiplier = 0.125f;
}
if (Input.GetButtonUp("Fire2"))
{
isaiming = false;
player.GetComponent<player>().sensMultiplier = 1f;
}
if (isaiming)
inHand.transform.position = aimpos.transform.position;
if (Input.GetButtonDown("Fire1") && firetimer < 1)
{
ps.Play();
Debug.Log("FIRE");
gameObject.GetComponent<Camera>().fieldOfView = ZOOM + 10;
inHand.transform.position = recoilpos.transform.position;
inHand.transform.rotation = recoilpos.transform.rotation;
firetimer = 2f;
transform.localRotation = Quaternion.Euler(transform.localRotation.x - 0.1f, transform.localRotation.y, transform.localRotation.z);
if (Physics.Raycast(aimpos.transform.position, transform.forward, out bullet, 9999999f))
{
if (bullet.collider.gameObject.layer == player1)
{
bullet.collider.gameObject.GetComponent<player>().Hp -= awpDamage;
}
else
{
Instantiate(spark, bullet.point, Quaternion.Euler(Vector3.zero));
}
}
}
}
}
}
void Reload()
{
Debug.Log("Reloading...");
if (inHand.tag == "pistol")
{
if (currentpistolMags != 0)
{
currentpistolMags -= 1;
currentpistolAmmo = pistolAmmo;
inHand.GetComponent<Animation>().Play("spinnyboi");
}
}
}
}