Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Weapon Pickup one weapon at a time

Discussion in 'Scripting' started by Robster95, Apr 8, 2020.

  1. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    152
    Hello, I am working on a weapon pickup system for my player character and i'm having some trouble.

    I made the same system but attached it to weapons and items but realized if I stand in the range of 2 items at the same time and try to pick them up it picks up both. I'm making a simple 2D side scrolling type game like Deadcells where you can only hold 1 weapon at a time. when the items have the script it makes both of them attach to the player at the same time.

    I decided to remake the script and add it to the player character but i'm confused on how to make it so only one item can be attached at a time.

    If anyone has any information on this please let me know! thank you

    P.S. this code below is attached to the weapons

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class ItemPickup : MonoBehaviour
    7. {
    8.     public bool PickupAllowed = false;
    9.     public float Pickupradius;
    10.     public Transform PickupSpot;
    11.     public LayerMask PlayerLayer;
    12.     public Transform NewPosition;
    13.     private bool IsHeld = false;
    14.     private Rigidbody2D rb;
    15.  
    16.  
    17.  
    18.  
    19.     public GameObject PlayerParent;
    20.  
    21.     void Start()
    22.     {
    23.         rb = gameObject.GetComponent<Rigidbody2D>();
    24.     }
    25.  
    26.  
    27.     void Update()
    28.     {
    29.         //check if the items in range
    30.         PickupCircle();
    31.  
    32.         //pickup the item
    33.  
    34.         if (IsHeld == false && PickupAllowed == true && Input.GetKeyDown(KeyCode.E) && gameObject == this.gameObject)
    35.         {
    36.             Pickup();
    37.         }
    38.  
    39.  
    40.         else if(IsHeld == true && Input.GetKeyDown(KeyCode.E))
    41.         {
    42.             Drop();
    43.         }
    44.     }
    45.  
    46.  
    47.     private void PickupCircle()
    48.     {
    49.         if(Physics2D.OverlapCircle(PickupSpot.position, Pickupradius, PlayerLayer))
    50.         {
    51.             PickupAllowed = true;
    52.         }
    53.  
    54.         else
    55.         {
    56.             PickupAllowed = false;
    57.         }
    58.     }
    59.  
    60.     private void OnDrawGizmos()
    61.     {
    62.         Gizmos.DrawWireSphere(PickupSpot.position, Pickupradius);
    63.     }
    64.  
    65.  
    66.  
    67.     private void Pickup()
    68.     {
    69.         rb.isKinematic = true;
    70.  
    71.         IsHeld = true;
    72.  
    73.         gameObject.transform.position = new Vector2(NewPosition.position.x, NewPosition.position.y);
    74.  
    75.         gameObject.transform.parent = PlayerParent.transform;
    76.  
    77.     }
    78.  
    79.  
    80.    
    81.     public void Drop()
    82.     {
    83.         IsHeld = false;
    84.  
    85.         gameObject.transform.parent = null;
    86.  
    87.         gameObject.transform.position = new Vector2(transform.position.x, transform.position.y);
    88.  
    89.         rb.isKinematic = false;
    90.     }
    91. }
    92.  
     
    depressedguyAF likes this.
  2. RLord321

    RLord321

    Joined:
    Feb 25, 2017
    Posts:
    28
    If you have a different script on each of the weapons, then yes, it will pick up as many objects that are in the range. If you had 10 weapons or items, it would pick up 10. The reason why is because the script is local to the actual object - weapon or item in your case. The item script can't see that the weapon has been picked up and vice versa.

    There's many ways to do this but essentially, you'll have to store the flag of if your player has a weapon or item on the Player script or variable. Something like:

    Code (CSharp):
    1. if( Player.HandsFree())
    2. {
    3.     Player.SetHandsFree( false);
    4.     // Add code to pick up the weapon or item
    5.  
    6. }
     
  3. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    152
    But how would this code look to see if there is a player holding something or if there are two objects next to each other how would it check to see if it can pick up one instead of the other?
     
  4. RLord321

    RLord321

    Joined:
    Feb 25, 2017
    Posts:
    28
    When the player picks up and item, you set the flag on the player saying that he/she is holding something. How do you want it to handle if there are two objects close to each other. You are in charge so it's whatever you want:
    1. Do you want the player to pick up the closest object (even though they are both in range)? Check the distance from the player and which ever is closer, pick that one up.
    2. Do you want to make the weapons have a higher priority than the items? If so, check the objects of the weapon type before you check the objects of the item type.
     
  5. depressedguyAF

    depressedguyAF

    Joined:
    Feb 11, 2021
    Posts:
    2
     
  6. depressedguyAF

    depressedguyAF

    Joined:
    Feb 11, 2021
    Posts:
    2
    oh this script is perfect, thanks to your scripts my game is 80% finish, thank for posting this script i been searching for pickup system for ages i finally it finally end thanks to your script, nice hope you see this comment
     
    Robster95 likes this.
  7. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    152
    glad I could help! lol