- Home /
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.
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 Get$$anonymous$$eyUp, rather than Get$$anonymous$$eyDown or GetButtonDown? Also, why not just subtract vectors to compare distance ins$$anonymous$$d 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 $$anonymous$$eyCode.E (or releasing it in your case). At least that way you'll be able to narrow things down.
I got a fully working inventory system. Don't have to post it here right? It works with an Inventory$$anonymous$$anager 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. $$anonymous$$y 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.
float distance = Vector3.Distance(playerTransform.position, transform.position); would run faster
oh wait nvm you're using js
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.
seriously it is useless helping some retard who cant use Debug.Log() and not ready to listen to ur words!!
Answer by Wolfie · Feb 07, 2012 at 04:33 PM
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.
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 $$anonymous$$ 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.
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!
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.
O$$anonymous$$, 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.
Yes, the problem exists in the distance calculation. If I do not check distance, the objects will get picked up.
Answer by TheGreatHooD · Feb 07, 2012 at 05:44 PM
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();
}
}
Your answer
Follow this Question
Related Questions
Perfect inventory code giving errors? [C#] 1 Answer
Creating an interactable inventory? 1 Answer
How to make things in my inventory able to drop and use? 1 Answer
c# Weapon Pickup Script 1 Answer
FPS Weapon inventory script help 0 Answers