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. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Question Equipped item is immediately thrown back down.

Discussion in 'Scripting' started by Sploomph, Nov 25, 2022.

  1. Sploomph

    Sploomph

    Joined:
    Aug 15, 2022
    Posts:
    10
    Code (CSharp):
    1.  
    2. public class iEq_Stick : MonoBehaviour, Equippable
    3. {
    4.     public bool handFull;
    5.     public bool equipped;
    6.     public Transform itemTransform;
    7.     public Transform hand;
    8.     public Transform cam;
    9.     public Rigidbody rb;
    10.     public BoxCollider coll;
    11.    
    12.  
    13.     void Equippable.Use()
    14.     {
    15.         Pickup();
    16.     }
    17.  
    18.     private void Update()
    19.     {
    20.        
    21.         if (Input.GetKeyDown(KeyCode.Mouse1))
    22.         {
    23.             Drop();
    24.         }
    25.     }
    26.  
    27.     private void Pickup()
    28.     {
    29.         handFull = true;
    30.  
    31.         itemTransform.SetParent(hand);
    32.         transform.localPosition = Vector3.zero;
    33.         transform.localRotation = Quaternion.Euler(Vector3.zero);
    34.         //transform.localScale = Vector3.one;
    35.  
    36.         rb.isKinematic = true;
    37.         coll.isTrigger = true;
    38.      
    39.     }
    40.  
    41.     private void Drop()
    42.     {
    43.         handFull = false;
    44.  
    45.         itemTransform.SetParent(null);
    46.  
    47.         rb.isKinematic = false;
    48.         coll.isTrigger = false;
    49.  
    50.         rb.velocity = rb.GetComponent<Rigidbody>().velocity;
    51.  
    52.         //not finished here
    53.     }
    54. }
    55.  
    Obviously not the whole script but when I click mouse1 to pick it up it immediately gets thrown back down. It gets picked up normally with Drop() commented out.

    So how would I add a pause between the actions?
     
    Last edited: Nov 25, 2022
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    2,868
    And how does
    Pickup()
    get called?
     
  3. Sploomph

    Sploomph

    Joined:
    Aug 15, 2022
    Posts:
    10
    Code (CSharp):
    1.  
    2. public class P_Raycast : MonoBehaviour
    3. {
    4.     public Camera cam;
    5.     private float rayLength = 3f;
    6.     public LayerMask interactableLayer;
    7.  
    8.     private void Update()
    9.     {
    10.         Ray rayName = new Ray(cam.transform.position, cam.transform.forward);
    11.  
    12.         Debug.DrawRay(rayName.origin, rayName.direction * rayLength);
    13.         RaycastHit hitInfo; //this stores our collision info
    14.         if (Physics.Raycast(rayName, out hitInfo, rayLength, interactableLayer))
    15.         {
    16.             if (hitInfo.transform.gameObject.tag == "Consumable" && Input.GetKeyDown(KeyCode.E))
    17.             {
    18.                 hitInfo.transform.GetComponent<Consumable>().Use();
    19.             }
    20.  
    21.             if (hitInfo.transform.gameObject.tag == "Equippable" && Input.GetKeyDown(KeyCode.Mouse1))
    22.             {
    23.                 hitInfo.transform.GetComponent<Equippable>().Use();
    24.             }
    25.  
    26.  
    27.         }
    28.     }
    29. }
    30.  
    From a raycast script when the player is looking at an eligible object and is pressing mouse1.
     
    Last edited: Nov 25, 2022
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,385
    GetKeyDown is when you press down the mouse click. If you're doing both actions on pressing mouse1, why are they in two different places?
     
  5. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,355
    Presumably the second script you posted is getting executed before the first script you posted. So it detects Mouse1 being pressed, calls Use() and thus PickUp(), runs the other scripts, detects Mouse1 being pressed, thus executes Drop().

    It would be easier to talk about the scripts if you didnt cut out their names. I'm also curious for why you separated those in the first place, like RadRedPanda mentioned. You can adjust the exeuction order of scripts, but oftentimes (not always) having to do so is rather an indicator for some bad architecture.
     
  6. Sploomph

    Sploomph

    Joined:
    Aug 15, 2022
    Posts:
    10
    Let me try to explain
    So there is a raycast script attached to the player cam. When it sees something on the Layer "Interactable" It then checks the tag. Depending on the tag (in this case "Equippable") you can press a button (Mouse1) and it will send a Use() command via interface to whatever unique script is attached to the object it sees.

    So what im trying to do is mouse1 to equip and also mouse1 to unequip. So i'm trying to find a way so that mouse1 on the unequip function (Drop()) isn't immediately trigged by the action of pressing mouse1 to equip (Use()).

    Also I separated them because the second script posted is the raycast one attached to the player, the first scipt is the one for a unique, individual equippable. And I basically wanted ithe player raycast script to send a generic Use() command to whatever individual, unique script is attached to the object it hits, if the correct use button is pressed.

    I also went ahead and went back and edited the names into them, mb.
     
    Last edited: Nov 25, 2022
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    2,868
    The way it's coded your item is always being 'dropped' even when not picked up.

    But to be honest the item itself shouldn't be be responsible for dropping itself. The same system for picking up items should also handle dropping them; which would make it trivial to solve this problem.
     
  8. Sploomph

    Sploomph

    Joined:
    Aug 15, 2022
    Posts:
    10
    Yeah i know but i'm just trying to get it to work first y'know. But anyway I ended up getting it working by adding a function that sets a bool "ready" to true and then Invoking said function after 1 second in Start. Then i added the bool in the Drop()'s if statement. I then set the bool to false in the Drop() method.

    Probably not the best way to do it, I know, but it works for now