- Home /
Question by
melvinweert · Oct 05, 2020 at 10:03 AM ·
networkingpickupmirror
Mirror Networking: Pickup Items
Hi. Im currently making an multiplayer game using Mirror's networking. I've made a little mechanice where you can pick up objects just like in half life/portal. So this works fine. The problem is its having some issues when being picked up in multiplayer. It sometimes invisible for other players or doesnt know which player is picking it up. Heres my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
public class Pickup : NetworkBehaviour
{
private GameObject Dest;
private bool hasPlayer;
private Controls controls;
[Client]
private void Start()
{
if (isLocalPlayer) return;
Dest = GameObject.FindWithTag("Destination");
Dest.SetActive(false);
}
private void Update()
{
if (hasPlayer && Input.GetKeyDown(KeyCode.E))
{
GetComponent<Rigidbody>().useGravity = false;
GetComponent<Rigidbody>().freezeRotation = true;
GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePosition;
this.transform.position = GameObject.Find("Destination").transform.position;
this.transform.parent = GameObject.Find("Destination").transform;
}
if (Input.GetKeyUp(KeyCode.E))
{
this.transform.parent = null;
GetComponent<Rigidbody>().useGravity = true;
GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
GetComponent<Rigidbody>().freezeRotation = false;
}
}
private void OnTriggerEnter(Collider col)
{
if (col.CompareTag("Player"))
{
hasPlayer = true;
}
}
private void OnTriggerExit(Collider col)
{
if (col.CompareTag("Player"))
{
hasPlayer = false;
}
}
private void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Static")
{
this.transform.parent = null;
GetComponent<Rigidbody>().useGravity = true;
GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
GetComponent<Rigidbody>().freezeRotation = false;
}
}
}
Comment
Answer by melvinweert · Oct 05, 2020 at 10:23 AM
Update: If the host picks up the box it works fine and the client player can also see the host picking it up. But if the client picks it up the box teleports in front of the host and doesnt move