- Home /
Picking up objects wont work on server
I'm working on a simple multiplayer games, player movement has worked, but having weapons that you can pick up is not working. Here is the script for picking up the weapon:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PickupDropItems : MonoBehaviour {
public Transform inv;
public Transform cam;
public InvControl invc;
public float throwForce;
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
Pickup();
}
if (Input.GetKeyDown(KeyCode.Q))
{
Drop();
}
}
void Pickup()
{
RaycastHit hit;
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, 10f) && hit.transform.gameObject.tag == "Item")
{
if(inv.GetChild(invc.selectedInvSlot).childCount == 0)
{
if (hit.transform.GetComponent<Rigidbody>() != null)
{
hit.transform.GetComponent<Rigidbody>().isKinematic = true;
}
if (hit.transform.GetComponent<gun>() != null)
{
hit.transform.GetComponent<gun>().player = gameObject.GetComponent<Player>();
hit.transform.GetComponent<gun>().cam = cam.gameObject;
hit.transform.GetComponent<gun>().animator = inv.GetComponent<Animator>();
}
if (hit.transform.GetComponent<Item>() != null)
{
for (int i = 0; i < hit.transform.GetComponent<Item>().collidersToDisable.Count; i++)
{
hit.transform.GetComponent<Item>().collidersToDisable[i].enabled = false;
}
}
hit.transform.SetParent(inv.GetChild(invc.selectedInvSlot));
hit.transform.position = inv.GetChild(invc.selectedInvSlot).position;
hit.transform.rotation = inv.GetChild(invc.selectedInvSlot).rotation;
}
}
}
void Drop()
{
if (inv.GetChild(invc.selectedInvSlot).childCount != 0)
{
inv.GetChild(invc.selectedInvSlot).GetChild(0).GetComponent<Rigidbody>().isKinematic = false;
for (int i = 0; i < inv.GetChild(invc.selectedInvSlot).GetChild(0).transform.GetComponent<Item>().collidersToDisable.Count; i++)
{
inv.GetChild(invc.selectedInvSlot).GetChild(0).transform.GetComponent<Item>().collidersToDisable[i].enabled = true;
}
if (inv.GetChild(invc.selectedInvSlot).GetChild(0).GetComponent<gun>() != null)
{
inv.GetChild(invc.selectedInvSlot).GetChild(0).GetComponent<gun>().player = null;
inv.GetChild(invc.selectedInvSlot).GetChild(0).GetComponent<gun>().cam = null;
inv.GetChild(invc.selectedInvSlot).GetChild(0).GetComponent<gun>().animator = null;
}
inv.GetChild(invc.selectedInvSlot).GetChild(0).GetComponent<Rigidbody>().AddForce((inv.GetChild(invc.selectedInvSlot).GetChild(0).forward * throwForce) + (inv.GetChild(invc.selectedInvSlot).GetChild(0).up * throwForce / 2.5f));
inv.GetChild(invc.selectedInvSlot).GetChild(0).SetParent(null);
}
}
}
When the host picks up the weapon, it will be reparented to the weapon holder, however, on the client side, the weapon's rigidbody doesn't seem to be kinematic, as it is supposed to be. When the client picks up the weapon, it doesn't seem to be reparented to the player's weapon holder and it's rigidbody is not kinematic. The weapon has a network identity that is neither server only or local player authority, and a network transform that syncs rigidbody 3D. if anyone would be able to help, that would be very appreciated.
From what i can see in your script your not sending any type of information to tell the other players your picking up or dropping anything. Im not familiar with uNet, which is what i believe you are using but I think that uNet uses RPCs/commands, which are remote procedure calls. I would recommend watching a few youtube videos on uNet so you have a better understanding of how multiplayer coding works,
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do I change weapons with Photon (FPS) C# 0 Answers
how to resize a decal 0 Answers
Creating interactable enviornment items that are usable by all players in Multiplayer 2 Answers