- Home /
Unity JS - Picking up objects
Hi, I'm sure similar questions have been asked but please bare with me as I'm new!
I'm creating an FPS game and require the ability to pick up items. I've heard the use of raycasts but I'm not sure how to script this. I'm hoping to make it so that when the character is in proximity of the object, or looking at it within proximity (whichever makes more sense) from them to pick it up so it is no longer there.
Additionally to this, I want there to be objects that can be opened only if the item required has already been picked up.
Any help would be greatly appreciated!
Update:
I tried the following code but it isn't working, any ideas? (I have a box collider on the object I want to interact with)
var rayCastLength = 5;
function Update () { var hit : RaycastHit; if (Physics.Raycast(transform.position, transform.forward, hit, rayCastLength)) { if(hit.collider.gameObject.tag == "ID") { hit.collider.gameObject.Destroy(); } } }
PS.I attached that script to the Main Camera within the First Person Controller.
Answer by Justin Warner · Dec 05, 2010 at 03:13 PM
http://www.youtube.com/watch?v=7abYR8W8ji4
That's for raycasts...
I'd personally do an if statement, check the distance between the item and the player, and then do another if in that if to check if the player has it, if he does, output it to screen, if he doesn't, then have it Destroy, then put it in his inventory...
I don't know much about raycasts, so I'd wait for someone more intelligent on that to post an answer, but I hoped I steered you a little bit =).
You did and thank you, I'll look in to RayCasting and all the things you suggested! :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613085800im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Weapon Pickup and change 2 Answers
Stop Object From Ignoring Collisions - Pick Up Rigidbody 0 Answers
pickup counter and timer help 0 Answers
How to pickup and equip an object? 0 Answers
eating candy, spitting it out. 1 Answer