x


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.

more ▼

asked Dec 05 '10 at 03:05 PM

Keira gravatar image

Keira
3 3 3 8

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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 =).

more ▼

answered Dec 05 '10 at 03:13 PM

Justin Warner gravatar image

Justin Warner
6.3k 19 27 65

You did and thank you, I'll look in to RayCasting and all the things you suggested! :)

Dec 05 '10 at 03:44 PM Keira
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3570
x149

asked: Dec 05 '10 at 03:05 PM

Seen: 2442 times

Last Updated: Dec 05 '10 at 05:24 PM