Unity FPS Object Pickup System

Поделиться
HTML-код
  • Опубликовано: 6 сен 2024
  • Hey there, It's been a while, but here's another tutorial. This tutorial is about a basic object pickup system to use. If you have any questions, I'll answer them in the comment section. Thanks for watching! And I hope this tutorial helps!
    By the way, for anyone looking - To disable/enable all children object colliders too, just duplicate the foreach loop you already have and instead of GetComponents, say GetComponentsInChildren.
    github.com/The...
    Unfortunately I don't have the actual unity project but I do have the two scripts in the GitHub.

Комментарии • 82

  • @giraffos
    @giraffos 2 года назад

    Hours of searching and ur vid is the one U NEED MORE SUBS

    • @rgba_dev
      @rgba_dev  2 года назад

      Made my day :) Thanks!

  • @trex6867
    @trex6867 2 года назад

    You are awesome at that age! Keep it up thanks for tutorial

  • @krazykase3419
    @krazykase3419 2 года назад

    this is the grab object script i needed

  • @tazitt7325
    @tazitt7325 Год назад

    You`ve helped me very much! Thank You!

    • @rgba_dev
      @rgba_dev  Год назад

      Your welcome! I'm happy it helped!
      Oh, also, I"m looking to improve the quality of my tutorials and I was wondering if there was anything I could do better?

    • @tazitt7325
      @tazitt7325 Год назад

      @@rgba_dev To be honest I don't see something that could be better than it is. I've Just new in unity). Just keep doing what you do and then you will see

  • @SkyRux0
    @SkyRux0 Год назад

    nice video keep going

  • @jonschld
    @jonschld 2 года назад

    Super helpful video, thanks

    • @rgba_dev
      @rgba_dev  2 года назад

      Happy it helped you :)

  • @randellricamara7727
    @randellricamara7727 2 года назад

    should i declare a public? so i the getcompenntsinchildren will work?

  • @V0rtex66
    @V0rtex66 2 года назад

    I have a problem. When I throw an object many times, it starts to stretch out for me. Has anyone encountered a similar problem?

    • @rgba_dev
      @rgba_dev  2 года назад

      That doesn't happen to me... Are you modifying the vertices of the object in anyway?

  • @flemi469
    @flemi469 3 месяца назад

    How to make this replicated to photon?

    • @rgba_dev
      @rgba_dev  3 месяца назад

      Hi, really sorry, I've never used Photon before so I unfortunately can't help you with that :(
      Try searching online - You can probably find some tutorials on photon.

  • @VladBuculei
    @VladBuculei 2 года назад

    Would it be possible to share the finished script? Thank you.

    • @rgba_dev
      @rgba_dev  2 года назад

      Yeah sure! I don't have the unity project itself, but here is a GitHub link for both scripts I used (one is the pickup script and the other is just the movement I have in the video)
      github.com/TheUnknown1050/Object-Pickup-System

    • @pawenizioek7804
      @pawenizioek7804 2 года назад

      @@rgba_dev after using, and checking code with video it doesn't work, when I hit play it freeze

    • @rgba_dev
      @rgba_dev  2 года назад

      @@pawenizioek7804 Are you getting any sort of error? The problem might be that you have the console set to "Error Pause".
      It might not be that script specifically, could you disable the object pick up scripts and see if the game is still freezing? If it is, then it is a problem with another script, or, as mentioned above, it is because of the Error Pause option in the console.
      Thanks for watching by the way!

  • @tameranian
    @tameranian 2 года назад

    Could this be modified in a way to work like how half life and/or portal handle picking up and throwing objects? If so, would you have any suggestions on how to do so?

    • @rgba_dev
      @rgba_dev  2 года назад

      Just to clarify, I’ve never played either so what exactly do you mean by half life or portal handling?

    • @tameranian
      @tameranian 2 года назад

      @@rgba_dev what I mean by that is when you pick up an object in either of those games it hovers right in front of you instead of being in the players hands

    • @rgba_dev
      @rgba_dev  2 года назад

      @@tameranian I think you could shoot a ray out forward from the camera at a set distance (You would have to experiment with the distance value) and the object holder is at the rays ending point. You could use the Vector3.Lerp or Vector3.SmoothDamp functions to make it feel like the object is hovering to the proper position. For example, when you move the camera, it would smoothly interpolate instead of snapping there.

  • @micheleguardabasso8162
    @micheleguardabasso8162 2 года назад +1

    hello beatiful your video can you please see my project because don't load in the correct position object i have a simple save system in unity

    • @rgba_dev
      @rgba_dev  2 года назад +1

      No problem, you can send the project on something like github.

  • @dewantara9683
    @dewantara9683 Год назад

    i haveing problem, NullReferenceException: Object reference not set to an instance of an object
    PickUpItem.PickUp () (at Assets/Script/PickUpItem.cs:49)
    PickUpItem.Update () (at Assets/Script/PickUpItem.cs:38)

    • @dewantara9683
      @dewantara9683 Год назад

      this is my code,
      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      using UnityEngine.UI;
      using TMPro;
      public class PickupObject : MonoBehaviour
      {
      public float pickUpRange;
      public float maxThrowForce;
      public float throwForceIncreaseSpeed;
      public Transform objectHolder;
      public LayerMask pickUpLayer;
      public GameObject throwBarGameObject;
      // Make sure to include UnityEngine.UI
      public Slider throwBar;
      public GameObject pickUpGameObject;
      // Make sure to include TMPro (if using text mesh pro)
      public TextMeshProUGUI pickUpText;
      private GameObject currentObject;
      private Rigidbody currentObjectRb;
      private float currentThrowForce;
      private void Start()
      {
      throwBar.maxValue = maxThrowForce;
      throwBarGameObject.SetActive(false);
      pickUpGameObject.SetActive(false);
      }
      private void Update()
      {
      PickUp();
      Throw();
      }
      private void PickUp()
      {
      if (currentObject != null)
      {
      return;
      }
      Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
      RaycastHit hitInfo;
      if (Physics.Raycast(ray, out hitInfo, pickUpRange, pickUpLayer))
      {
      if (currentObject == null)
      {
      pickUpGameObject.SetActive(true);
      pickUpText.text = hitInfo.collider.gameObject.name;
      }
      if (Input.GetKeyDown(KeyCode.E))
      {
      currentObject = hitInfo.collider.gameObject;
      currentObjectRb = currentObject.GetComponent();
      currentObject.transform.parent = objectHolder;
      currentObject.transform.localPosition = Vector3.zero;
      currentObject.transform.localEulerAngles = new Vector3(0, 0, 0);
      foreach (Collider collider in currentObject.GetComponents())
      {
      collider.enabled = false;
      }
      currentObjectRb.isKinematic = true;
      }
      }
      else
      {
      pickUpGameObject.SetActive(false);
      pickUpText.text = "";
      }
      }
      private void Throw()
      {
      if (currentObject == null)
      {
      return;
      }
      if (Input.GetKey(KeyCode.Q))
      {
      throwBarGameObject.SetActive(true);
      if (currentThrowForce maxThrowForce)
      {
      currentThrowForce = maxThrowForce;
      }
      if (Input.GetKeyUp(KeyCode.Q))
      {
      currentObject.transform.parent = null;
      currentObjectRb.isKinematic = false;
      currentObjectRb.AddForce(currentObject.transform.forward * currentThrowForce, ForceMode.Impulse);
      foreach (Collider collider in currentObject.GetComponents())
      {
      collider.enabled = true;
      }
      currentObject = null;
      currentObjectRb = null;
      currentThrowForce = 0;
      throwBar.value = 0;
      throwBarGameObject.SetActive(false);
      }
      }
      }

    • @dewantara9683
      @dewantara9683 Год назад

      my problem is solved, Thank You for the code you created

    • @rgba_dev
      @rgba_dev  Год назад

      @@dewantara9683 Your welcome, I'm happy you fixed the problem!

    • @dewantara9683
      @dewantara9683 Год назад

      ​@@rgba_dev

  • @Cooked_Pigg
    @Cooked_Pigg Год назад

    wich version are you using

    • @rgba_dev
      @rgba_dev  Год назад

      In the video I was using Unity 2020.3.13f
      You can check by looking in the top left, right above File, Edit, Assets, etc.
      It says the name of the project/scene followed by the build type, followed by the Unity version you are on.

  • @randellricamara7727
    @randellricamara7727 2 года назад

    im having a problem when i pick up the objects it will not display the front of the object it depends on the position you pick it

    • @rgba_dev
      @rgba_dev  2 года назад

      Make sure you remember to add these lines:
      currentObject.transform.parent = objectHolder;
      currentObject.transform.localPosition = Vector3.zero;
      currentObject.transform.localEulerAngles = new Vector3(0, 0, 0);
      Those lines basically just make sure that the object becomes a child of the object holder (also make sure you have the object holder) and that the position is the center of the object holder, and finally that the local rotation gets reset to 0 on all axis.
      Let me know if it works!

    • @randellricamara7727
      @randellricamara7727 2 года назад

      @@rgba_dev it works! thankyou so much my friend

    • @rgba_dev
      @rgba_dev  2 года назад

      ​@UCo8FCeT63aL0DxsR5Pcz7AQ No problem! github.com/TheUnknown1050/Object-Pickup-System Those are the two scripts, player movement and object pickup system.
      Good luck on your projects, I know that when starting unity it could be quite a challenge to do things, but I'm sure you can achieve it!​

    • @rgba_dev
      @rgba_dev  2 года назад

      Lol for some reason when i tried mentioning your name it came out like that, sorry about that

    • @rgba_dev
      @rgba_dev  2 года назад

      @@randellricamara7727 Your welcome :) !! I'm happy it worked out for you!

  • @partydaclown6706
    @partydaclown6706 Год назад

    is this code copyrighted? Can i use it?

    • @rgba_dev
      @rgba_dev  Год назад +1

      Yeah, you can use it! The code is linked in the description, just in case if you want it.

  • @calisthenicswithandrew7183
    @calisthenicswithandrew7183 2 года назад

    I got this error:NullReferenceException: Object reference not set to an instance of an object
    Can you please help me?

    • @rgba_dev
      @rgba_dev  2 года назад

      Can you please post the line that you are getting this error on? (Not the line number, but what the actual line says)

    • @calisthenicswithandrew7183
      @calisthenicswithandrew7183 2 года назад

      I don't know what you mean, I don't know how to code, I just watch youtube tutorials and make a fun game

    • @rgba_dev
      @rgba_dev  2 года назад

      ​@@calisthenicswithandrew7183 Press on the error in unity and then it will say the number of the line that has the error, like ObjectPickupSystem.cs:linenumber
      Go to that line in the script and copy paste it here. I need to know which line it is to see whats causing the problem

    • @calisthenicswithandrew7183
      @calisthenicswithandrew7183 2 года назад

      I tried to remake the script but now it shows me this error:Assets\Scripts\ObjectPickupSystem.cs(17,9): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

    • @rgba_dev
      @rgba_dev  2 года назад

      @@calisthenicswithandrew7183 Please paste line 17 of your script here, or double click the error in unity and it will take you to that line, and copy paste it here.

  • @ather12345
    @ather12345 2 года назад

    What the freak u have 62 sub only that video and this video? 😭😭😭😭😭😭☠️

  • @chesteeer
    @chesteeer 2 года назад +1

    i got this error at the text part: NullReferenceException: Object reference not set to an instance of an object
    ObjectPickupSystem.PickUp () (at Assets/Scripts/ObjectPickupSystem.cs:46)

    • @rgba_dev
      @rgba_dev  2 года назад

      Did you reference the text object in the inspector? That might be problem.

    • @chesteeer
      @chesteeer 2 года назад

      @@rgba_dev i cant 😩 thats the problem

    • @rgba_dev
      @rgba_dev  2 года назад

      @@chesteeer Ohh ok. Then you may have forgotten to add the text component to the text object and then reassign the variable… if that doesn’t work it may be a bug but I’m not sure

    • @chesteeer
      @chesteeer 2 года назад

      @@rgba_dev i have a skin selector on my game, and the skins need to be prefabs, when the skin is in the hierarchy i can put the text, but when i delete the prefab from there i cant put the text, so i think the text is imposible to do

    • @rgba_dev
      @rgba_dev  2 года назад

      @@chesteeer Well the text is a UI element so it must be going under the canvas.
      But if the canvas object is under the player skin, then this code should work.
      So create a variable (not in any functions): public Transform pickUpTextParent;
      Then in the Start function, before setting the pickUpText variable to be inactive, write this code:
      GameObject instantiatedPickUpText = new GameObject("Pick Up Text");
      instantiatedPickUpText.AddComponent();
      pickUpText = instantiatedPickUpText.GetComponent();
      pickUpText.transform.SetParent(pickUpTextParent);
      pickUpText.transform.localPosition = Vector3.zero;
      Then set the pickUpTextParent variable to the canvas. Currently the text will be spawned into the center but you can change that by changing the Vector from Vector3.zero to what you want.