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
0
Question by Snake111 · Jul 02, 2015 at 10:31 AM · script.

Inventory system

Hi guys I just downloaded a inventory script (Diabolic-Free) on asset store. I want to change the state [ if (Input.GetMouseButtonDown(0)) ] to OnTriggerEnter can someone help me please.

void Update() { int litem = 1 << 8;

     ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     if (Physics.Raycast(ray, out hitInfo, 50000.0f,litem))
     {
         ItemClass ic = hitInfo.transform.gameObject.GetComponent<ItemClass>();

         
         if (Input.GetMouseButtonDown(0))
         {
            if (ic.isEquiped != false) return;
            AddItemToInventory(hitInfo.transform.gameObject.GetComponent<ItemClass>());
            Destroy(hitInfo.transform.gameObject);
         }
     }
 }
Comment
Add comment · Show 1
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 Ibzy · Jul 02, 2015 at 10:41 AM 0
Share

Are you sure you're looking to change teh right part of the script? I imagine you're looking to show inventory when colliding with an object rather than by clicking the mouse? If so this would be the Physics.Raycast part I think.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Snake111 · Jul 03, 2015 at 12:11 AM

I already have a pickup item when press keydown e so I want to set active this script by colliding enter (ontriggerenter) so I dont have to press 2 time e to activate these 2 script Also I want to open/close the inventory with i key. here is the long script.

using UnityEngine; using System.Collections;

public class MyInventory : MonoBehaviour { private RaycastHit hitInfo; private bool hashit = false; private Ray ray;

 public Texture item;
 public Texture defaultTexture;
 int gridWidth = 10;
 int gridHeight = 10;

 bool rmouseDown = false;
 bool lmousedown = false;


 bool bWeaponEquiped = false;//test
 bool bShieldEquiped = false;
 bool bR1Equiped = false;
 bool bR2Equiped = false;
 bool bAmuletEquiped = false;
 bool bBeltEquiped = false;
 bool bBootsEquiped = false;
 bool bGlovesEquiped = false;

 public GUIStyle lblItemDescrStyle;


 public struct Inventar
 {
     public string itemName;
     public Texture itemTexture;
     public int width;
     public int height;
     public bool drawn;
     public string opis;
     public bool occupiedCell;
     public int firstCell;
 }

public Inventar[] charInventory;

 public int invL;
 public int invT;


 void Start()
 {
     charInventory = new Inventar[gridWidth * gridHeight];
     //invL = invT = 20;
 }

 void Update()
 {
     int litem = 1 << 8;


     ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     if (Physics.Raycast(ray, out hitInfo, 50000.0f,litem))
     {
         ItemClass ic = hitInfo.transform.gameObject.GetComponent<ItemClass>();

         
         if (Input.GetMouseButtonDown(0))
         {
            if (ic.isEquiped != false) return;
            AddItemToInventory(hitInfo.transform.gameObject.GetComponent<ItemClass>());
            Destroy(hitInfo.transform.gameObject);
         }
     }
 }



 public int AddItemToInventory(ItemClass item)
 {
     int intialIndexLocation = -1; 
     if(item != null)
     {
         ItemClass temp =   item;      //Set up a temporary item to compare with

         if (SlotsRemaining() >= temp.height * temp.width)    
         {
             bool firstFoundLocation = true;                  
                                                              
             int count = 0;                                   
                                                                
             for(int i = 0; i < gridWidth-(temp.width-1); i ++) 
             {
                 for(int t = 0; t < (gridHeight)-(temp.height-1); t++)    
                 {
                     if (charInventory[i + (gridHeight * t)].occupiedCell != true)  
                     {
                         int neededCount = temp.height*temp.width;          
                         for(int j = 0; j < temp.width; j++)                
                         {
                             if (charInventory[i + (gridHeight * t) + j].occupiedCell != true)
                                                                                         
                             {
                                 for( int k = 0; k < temp.height; k++)   
                                 {
                                     if (charInventory[i + (gridHeight * (t + k)) + j].occupiedCell != true)
                                     {
                                         if(firstFoundLocation) 
                                         {
                                             firstFoundLocation = false; 
                                                                         
                                             intialIndexLocation = i+(gridWidth*t); 
                                         }
                                         count+=1;     
                                         if(count == neededCount)
                                         {
      
                                             break;
                                         }
                                     }
                                 }
                             }
                         }
                         if(count >= neededCount)
                         {
                             break;
                         } else if(count < neededCount)
                         {
                             firstFoundLocation = true;
                             count = 0;
                             intialIndexLocation = -1;
                         }
                     }
                 }  
             }
         }

         if (SlotsRemaining() == charInventory.Length)  
         {
             intialIndexLocation = 0; 
         }
         if(intialIndexLocation > -1) 
         {
             bool whentodraw=true;
             for(int i = 0; i < temp.width; i++)
             {
                 for(int t = 0; t < temp.height; t++)
                 {

                     charInventory[intialIndexLocation + i + (gridHeight * t)].drawn = true;
                     if (whentodraw) charInventory[intialIndexLocation + i + (gridHeight * t)].drawn = false;

                     charInventory[intialIndexLocation + i + (gridHeight * t)].itemName = item.itemName;
                     charInventory[intialIndexLocation + i + (gridHeight * t)].itemTexture = item.itemTexture;
                     charInventory[intialIndexLocation + i + (gridHeight * t)].width = item.width;
                     charInventory[intialIndexLocation + i + (gridHeight * t)].height = item.height;
                     charInventory[intialIndexLocation + i + (gridHeight * t)].opis = item.opis;
                     charInventory[intialIndexLocation + i + (gridHeight * t)].occupiedCell = true;
                     charInventory[intialIndexLocation + i + (gridHeight * t)].firstCell = intialIndexLocation;
                     whentodraw=false;
                 }
             }
         }
     }
     return intialIndexLocation;
 }


 void DeleteFromInventory(string itmNam, int fc)
 {
     for (int i = 0; i < charInventory.Length; ++i)
     {
         if (charInventory[i].itemName == itmNam && charInventory[i].firstCell == fc)
         {
             charInventory[i].occupiedCell = false;
         }

     }
 }

        
 string txtWidth = "";
 string txtHeight = "";
        
 void OnGUI()
 {


     //=======================================================character inventory=========================================
     for(int i = 0; i < gridWidth; i ++) 
     {  
         for(int t = 0; t < gridHeight; t++)
         {
             if (charInventory[i + (gridHeight * t)].occupiedCell != true)
             {
                 GUI.DrawTexture(new Rect((invL + (i * 30)), invT + (t * 30), 30, 30), defaultTexture);
             }
             else
             {
                 Inventar temp = charInventory[i + (gridHeight * t)];
                 if(!temp.drawn)
                 {
                     int w = temp.width;
                     int h = temp.height;

                     Rect buttonRect = new Rect((invL + (i * 30)), invT + (t * 30), 30 * w, 30 * h);
                     if (buttonRect.Contains(RevertMousePosValue()))
                     {
                         GUI.Label(new Rect(Screen.width / 2 - 100, 60, 200, 200), temp.opis.Replace("NEWLINE", "\n"), lblItemDescrStyle);
                     }


                     if (GUI.Button(buttonRect, temp.itemTexture))
                     {
                         if (Input.GetMouseButtonUp(0))
                         {

                             if (!bWeaponEquiped)//equip weapon
                             {
                                 GameObject HandGr = GameObject.Find("HandGrip");
                                 Vector3 gp = HandGr.transform.position;
                                 Instantiate(Resources.Load(temp.itemName), gp, Quaternion.identity);//no 2 prefabs same name
                                 GameObject cloned = GameObject.Find(temp.itemName + "(Clone)");
                                 cloned.transform.parent = HandGr.transform;
                                 cloned.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
                                 DeleteFromInventory(temp.itemName, temp.firstCell);
                                 bWeaponEquiped = true;
                                 ItemClass ic = cloned.GetComponent<ItemClass>();
                                 ic.isEquiped = true;
                             }

                         }
                         else if (Input.GetMouseButtonUp(1))
                         {
                             Vector3 rp = new Vector3(Random.Range(-1.0F, 1.0F), 0, Random.Range(-1.0F, 1.0F));
                             GameObject plyr = GameObject.Find("Player");
                             Vector3 PlayerPos = plyr.transform.position;
                             Instantiate(Resources.Load(temp.itemName), PlayerPos + rp, Quaternion.identity);//no 2 prefabs same name
                             DeleteFromInventory(temp.itemName, temp.firstCell);

                         }
                     }

                     temp.drawn = true;
                 }
             }
         }
     }
 }
 public int SlotsRemaining()
 {
     int count = 0;
     for (int i = 0; i < charInventory.Length; i++)
     {
         if (charInventory[i].occupiedCell != true)
         {
             count++;
         }
     }
     return count;
 }
 Vector2 RevertMousePosValue()
 {
     Vector2 mousePos =(Input.mousePosition);
     Vector2 newMousePos;
     newMousePos.x = mousePos.x;
     newMousePos.y = -mousePos.y + Screen.height;
     return newMousePos;
 }

}

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

shadow fuzziness 0 Answers

How to make object showing depend on target? 1 Answer

How to respawn an object 1 Answer

Manual Audio Listener Read 0 Answers

Editor Import Script for Platform Settings 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