Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by TheGreatHooD · Feb 07, 2012 at 11:31 AM · fpsinventory

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.

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wolfie · Feb 07, 2012 at 03:33 PM 0
Share

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.

avatar image TheGreatHooD · Feb 07, 2012 at 03:38 PM 0
Share

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.

avatar image gabs · Feb 07, 2012 at 04:04 PM 1
Share

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

oh wait nvm you're using js

avatar image Wolfie · Feb 07, 2012 at 04:07 PM 0
Share

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.

avatar image flamy · Feb 07, 2012 at 04:09 PM 0
Share

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

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheGreatHooD · Feb 07, 2012 at 04:37 PM 0
Share

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.

avatar image Wolfie · Feb 07, 2012 at 04:42 PM 0
Share

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!

avatar image TheGreatHooD · Feb 07, 2012 at 04:45 PM 0
Share

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.

avatar image Wolfie · Feb 07, 2012 at 04:51 PM 0
Share

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.

avatar image TheGreatHooD · Feb 07, 2012 at 04:53 PM 0
Share

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

Show more comments
avatar image
0

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(); 
    } 
 } 


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges