- Home /
 
 
               Question by 
               melvinweert · Oct 05, 2020 at 12:46 PM · 
                objectstagspickups  
              
 
              Pickup Objects
Hi. Im currently working on a multiplayer puzzle game. I'm currently working on a game mechanic which allows you to pick up objects like half life and portal. Now since its a multiplayer game my only problem is that it sometimes is being picked up by the wrong player. So I was wondering how to let only the player thats closest to the object be able to pick it up. Here's my code:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using Mirror;
 
 public class Pickupables : MonoBehaviour
 {
     private GameObject Dest;
     private bool canPickup;
     private Controls controls;
     private GameObject Pickup;
     private bool pickedUp = false;
 
     // Start is called before the first frame update
     void Start()
     {
         Pickup = GameObject.FindGameObjectWithTag("Pickup");
         Dest = GameObject.FindGameObjectWithTag("Destination");
     }
 
     // Update is called once per frame
     void Update()
     {
         if (!pickedUp && canPickup && Input.GetKeyDown(KeyCode.E))
         {
             Pickup.GetComponent<Rigidbody>().useGravity = false;
             Pickup.GetComponent<Rigidbody>().freezeRotation = true;
             Pickup.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePosition;
             Pickup.transform.position = GameObject.FindGameObjectWithTag("Destination").transform.position;
             Pickup.transform.parent = GameObject.FindGameObjectWithTag("Destination").transform;
             pickedUp = true;
         }
 
         if (pickedUp && Input.GetKeyUp(KeyCode.E))
         {
             Pickup.transform.parent = null;
             Pickup.GetComponent<Rigidbody>().useGravity = true;
             Pickup.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
             Pickup.GetComponent<Rigidbody>().freezeRotation = false;
             pickedUp = false;
         }
     }
 
     private void OnTriggerEnter(Collider col)
     {   //If "Player" goes inside of the trigger he can pick up the object. (Can Reach Object)
         if (col.CompareTag("Pickup"))
         {
             canPickup = true;
         }
     }
 
     private void OnTriggerExit(Collider col)
     {   //If "Player" goes outside of the trigger he can't pick up the object. (Can't Reach Object)
         if (col.CompareTag("Pickup"))
         {
             canPickup = false;
         }
     }
 
     private void OnCollisionEnter(Collision other)
     {
         if (other.gameObject.tag == "Static")
         {
             Pickup.transform.parent = null;
             Pickup.GetComponent<Rigidbody>().useGravity = true;
             Pickup.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
             Pickup.GetComponent<Rigidbody>().freezeRotation = false;
             pickedUp = false;
         }
     }
 }
 
 
              
               Comment
              
 
               
              Your answer
 
             Follow this Question
Related Questions
AI- trouble making NPC attack effect NPCs differently than player 1 Answer
Same objects doing the same thing 0 Answers
Selecting multiple tags 1 Answer
Finding the distance between objects 0 Answers
Can't get more than one object. 1 Answer