x


FPS picking up items

Hi,,

I've got some trouble picking up items in FPS. I've got a FULLY working inventory system. Only I can't call the Loot() function:

function Update () 
{ 
   distance = Mathf.Sqrt((playerTransform.position - transform.position).sqrMagnitude); 

   if(Input.GetKeyUp(KeyCode.E) && distance <= 4.0)
   { 
      Debug.Log("Looting!");
      Loot(); 
   } 
} 

I now got this. But this isn't working. I want to get close to the object, Press E and then call the Loot() function.

more ▼

asked Feb 07 '12 at 11:31 AM

TheGreatHooD gravatar image

TheGreatHooD
-9 3 3 4

Your fully working inventory system can't call one of its functions? I'd hate to see what you call broken! :D

Anyway, although you haven't provided enough information for me to give a definitive answer, there are a few things which strike me as odd or missing. Why are you using GetKeyUp, rather than GetKeyDown or GetButtonDown? Also, why not just subtract vectors to compare distance instead of square roots and so on? Finally what is this script attached to, and is your fully working inventory system in this same script, or in a separate one, or on another object entirely?

By simplifying things until it works you can then build it back up again. Personally I'd start by commenting out the distance condition in your 'if' statement and see if you can make it call Loot() by just pressing KeyCode.E (or releasing it in your case). At least that way you'll be able to narrow things down.

Feb 07 '12 at 03:33 PM Wolfie

I got a fully working inventory system. Don't have to post it here right? It works with an InventoryManager which is just an empty GameObject. I have attached the inventory script to it. Then I have another script for the object I want to pick up. Here is an function called Loot who while handle things further. My problem is that I can't actually call this Loot() function properly. I want to call it if i'm next to an object, then you press E and you actually pick this item up and put it in the inventory.

Feb 07 '12 at 03:38 PM TheGreatHooD

float distance = Vector3.Distance(playerTransform.position, transform.position); would run faster

oh wait nvm you're using js

Feb 07 '12 at 04:04 PM gabs

I'm afraid you're just repeating things there. What I'm trying to say is that I need a bit more info. You don't need to post the inventory script, but it's important to know where it is in your game. If the script you posted above is just a snippet of the fully working inventory system, then you can call the function as you did. If it's on another script (or another script on another object) then you need to use dot syntax to call it. That's why I asked about it.

Feb 07 '12 at 04:07 PM Wolfie

seriously it is useless helping some retard who cant use Debug.Log() and not ready to listen to ur words!!

Feb 07 '12 at 04:09 PM flamy
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Alright, I'll break it all down and hopefully we can get it to work. Try creating a new variable to hold your fully working inventory system's script (obviously change the names of variables to whatever you prefer and make sure your GetComponent references your script's actual name - it's probably not called FullyWorkingInventorySystemScript, as that's a silly name).

var fullyWorkingInventorySystemObject : GameObject;

Then call your function like this;

fullyWorkingInventorySystemObject.GetComponent("FullyWorkingInventorySystemScript").Loot();

Drag whatever object you've attached your script to onto that exposed variable, and test to see if it now works. Fix any minor errors that the debug log might throw back at you (eg. spelling errors and missing semicolons). Once you've done that and tested it, post back here to say if it's working or not. If not, then we'll try something else.

more ▼

answered Feb 07 '12 at 04:33 PM

Wolfie gravatar image

Wolfie
106 2 2 5

Wait, you've got me all wrong. The problem is in the fact that I can't call the function with my 'E' key. The problem doesn't exist in 1 of the inventory scripts but in my script that calls it.

I walk to an object. Press E. But then nothing happens, BECAUSE in the function to call the Loot function, that is incorrect. Shown in the example above, is HOW I call this Loot() function.

So if anybody can provide me an workable script to call functions if you're standing next to an in-game object.

Feb 07 '12 at 04:37 PM TheGreatHooD

Uh oh, asking for scripts is asking for trouble...

But wait, what? I'm getting confused now. I never suggested modifying your inventory script. We've established that it's fully working!

Feb 07 '12 at 04:42 PM Wolfie

Right, but I know how to call my function. But the problem is which steps before calling the Loot function. That is : standing right next to it and press E to pick up

That is what I thought I did with the script above, but it doesnt do anything.

Feb 07 '12 at 04:45 PM TheGreatHooD

OK, let's troubleshoot this from step one. Temporarily replace the script you posted above with;

function Update (){ Loot();
}

Does this call the Loot() function over and over again? If it does, then the problem is to do with your E key, your distance calculation or something else. If it doesn't work then do the thing I suggested in my original answer.

Feb 07 '12 at 04:51 PM Wolfie

Yes, the problem exists in the distance calculation. If I do not check distance, the objects will get picked up.

Feb 07 '12 at 04:53 PM TheGreatHooD
(comments are locked)
10|3000 characters needed characters left

It's working:

function Update () 
{ 

   var dist = Vector3.Distance(playerTransform.position, thisTransform.position);
   if(Input.GetKeyUp(KeyCode.E) && dist < 5.0)
   { 
      Debug.Log("Looting!");
      Loot(); 
   } 
} 
more ▼

answered Feb 07 '12 at 05:44 PM

TheGreatHooD gravatar image

TheGreatHooD
-9 3 3 4

(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:

x1171
x223
x8

asked: Feb 07 '12 at 11:31 AM

Seen: 1126 times

Last Updated: Feb 07 '12 at 06:09 PM