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.
Hours of searching and ur vid is the one U NEED MORE SUBS
Made my day :) Thanks!
You are awesome at that age! Keep it up thanks for tutorial
Thanks :p
this is the grab object script i needed
thanks :)
You`ve helped me very much! Thank You!
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?
@@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
nice video keep going
Thanks!
Super helpful video, thanks
Happy it helped you :)
should i declare a public? so i the getcompenntsinchildren will work?
I have a problem. When I throw an object many times, it starts to stretch out for me. Has anyone encountered a similar problem?
That doesn't happen to me... Are you modifying the vertices of the object in anyway?
How to make this replicated to photon?
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.
Would it be possible to share the finished script? Thank you.
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
@@rgba_dev after using, and checking code with video it doesn't work, when I hit play it freeze
@@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!
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?
Just to clarify, I’ve never played either so what exactly do you mean by half life or portal handling?
@@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
@@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.
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
No problem, you can send the project on something like github.
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)
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);
}
}
}
my problem is solved, Thank You for the code you created
@@dewantara9683 Your welcome, I'm happy you fixed the problem!
@@rgba_dev
wich version are you using
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.
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
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!
@@rgba_dev it works! thankyou so much my friend
@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!
Lol for some reason when i tried mentioning your name it came out like that, sorry about that
@@randellricamara7727 Your welcome :) !! I'm happy it worked out for you!
is this code copyrighted? Can i use it?
Yeah, you can use it! The code is linked in the description, just in case if you want it.
I got this error:NullReferenceException: Object reference not set to an instance of an object
Can you please help me?
Can you please post the line that you are getting this error on? (Not the line number, but what the actual line says)
I don't know what you mean, I don't know how to code, I just watch youtube tutorials and make a fun game
@@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
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
@@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.
What the freak u have 62 sub only that video and this video? 😭😭😭😭😭😭☠️
Give the kid a break.
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)
Did you reference the text object in the inspector? That might be problem.
@@rgba_dev i cant 😩 thats the problem
@@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
@@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
@@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.