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 ibouchallikht · Jan 07, 2021 at 08:39 PM · controllerswitchplaystationslotnext

Select slot with same key

Hello, I have an inventory in my game and when the player plays with controller, it switches to the controller input. Well with the keyboard, it is simple. the player can select which slot he want to hold by pressing the number pad 1 till 4 (I have only 4 slots) But with the ps4 controller, I stuck with the problem since I want the slot switch to the next slot with the R1 button and to switch to the previous one, you have to press the L1. If you press R1 while you hold the 4th slot, it needs to switch back to slot 1.

I really need help with this one. Can someone help me out with this one?

This is what I got so far...

 public List<SlotsSelector> slotsSelectors;
         public EquipArea equipArea;
         public Inventory inventory;
 
         public delegate void OnSelectSlot(int indexOfSlot);
         public event OnSelectSlot onSelectSlot;
 
         void Start()
         {
             inventory = GetComponentInParent<Inventory>();
 
             for (int i = 0; i < slotsSelectors.Count; i++)
             {
                 onSelectSlot += slotsSelectors[i].Select;
             }
 
             onSelectSlot?.Invoke(0);
         }
 
         void Update()
         {
             if (!inventory || !equipArea || inventory.lockInventoryInput) return;
 
             for (int i = 0; i < slotsSelectors.Count; i++)
             {    
                 if (Input.GetButtonDown("PS4_R1") && slotsSelectors[i].indexOfSlot >= 0 && slotsSelectors[i].indexOfSlot <= 2 && (inventory && !inventory.IsLocked() && !inventory.isOpen && inventory.canEquip))
                 {
                     slotsSelectors[i].indexOfSlot ++;
                 }
 
                 if (Input.GetButtonDown("PS4_L1") && (inventory && !inventory.IsLocked() && !inventory.isOpen && inventory.canEquip))
                 {
                     if (slotsSelectors[i].indexOfSlot < equipArea.equipSlots.Count && slotsSelectors[i].indexOfSlot >= 0)
                     {
                         equipArea.SetEquipSlot(slotsSelectors[i].indexOfSlot);
                         onSelectSlot?.Invoke(slotsSelectors[i].indexOfSlot);
                     }
                 }
 
                 if (slotsSelectors[i].equipDisplay != null && slotsSelectors[i].indexOfSlot < equipArea.equipSlots.Count && slotsSelectors[i].indexOfSlot >= 0)
                 {
                     if (equipArea.equipSlots[slotsSelectors[i].indexOfSlot].item != slotsSelectors[i].equipDisplay.item)
                     {
                         slotsSelectors[i].equipDisplay.AddItem(equipArea.equipSlots[slotsSelectors[i].indexOfSlot].item);
                     }
                     else if (equipArea.equipSlots[slotsSelectors[i].indexOfSlot].item == null && slotsSelectors[i].equipDisplay.hasItem)
                     {
                         slotsSelectors[i].equipDisplay.RemoveItem();
                     }
                 }
             }
         }
 
         [System.Serializable]
         public class SlotsSelector
         {
             public int indexOfSlot;
             public EquipmentDisplay equipDisplay;
             public bool selected;
             public void Select(int indexOfSlot)
             {
                 if (this.indexOfSlot != indexOfSlot && selected)
                 {
                     equipDisplay.onDeselect.Invoke();
                     selected = false;
                 }
                 else if (this.indexOfSlot == indexOfSlot && !selected)
                 {
                     equipDisplay.onSelect.Invoke();
                     selected = true;
                 }
             }
         }


I'm really dizzy right now, just trying to figure this out.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by yummy81 · Jan 07, 2021 at 10:04 PM

@ibouchallikht I removed a bunch of your code, to minimize it and create a sort of starting point for further development for you. This code takes into consideration how to wrap around the index of your slots. Tinker with it and look at the changes in the inspector. From now on you can built upon it, adding further logic step by step.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Game : MonoBehaviour
 {
     public List<SlotsSelector> slotsSelectors = new List<SlotsSelector>();
     public delegate void OnSelectSlot(int indexOfSlot);
     public event OnSelectSlot onSelectSlot;
     
     public int index = 0;
     
     void Start()
     {
         for(int i = 0; i < slotsSelectors.Count; i++)
         {
             onSelectSlot += slotsSelectors[i].Select;
         }
         
         onSelectSlot?.Invoke(index);
     }
     
     void Update()
     {
         if (Input.GetButtonDown("PS4_R1"))
         {
             index = (++index)%slotsSelectors.Count; 
             onSelectSlot?.Invoke(index);
         }
         else if (Input.GetButtonDown("PS4_L1"))
         {
             index = (--index + slotsSelectors.Count)%slotsSelectors.Count; 
             onSelectSlot?.Invoke(index);
         }
     }
 }
 
 [System.Serializable]
 public class SlotsSelector
 {
     public int indexOfSlot;
     public EquipmentDisplay equipDisplay;
     public bool selected;
     
     public void Select(int indexOfSlot)
     {
         selected = (this.indexOfSlot == indexOfSlot);
         
         if (!selected)
         {
             equipDisplay.onDeselect.Invoke();
         }
         else
         {
             equipDisplay.onSelect.Invoke();
         }
     }
 }
 
Comment
Add comment · Show 1 · 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 ibouchallikht · Jan 12, 2021 at 03:50 AM 0
Share

This one didn't works fine, but I've seen a simple code in this one that fixed my problem! Thank you!!

These are the new lines for the R1 and L1

 if (Input.GetButtonDown("PS4_R1") && (inventory && !inventory.IsLocked() && !inventory.isOpen && inventory.canEquip))
                 {
                     IndexSlot ++;
                     if (IndexSlot > 3) IndexSlot = 0;
 
                     if (slotsSelectors[i].indexOfSlot < equipArea.equipSlots.Count && slotsSelectors[i].indexOfSlot >= 0)
                     {
                         equipArea.SetEquipSlot(IndexSlot);
                         onSelectSlot?.Invoke(IndexSlot);
                     }
                 }
 
                 if (Input.GetButtonDown("PS4_L1") && (inventory && !inventory.IsLocked() && !inventory.isOpen && inventory.canEquip))
                 {
                     IndexSlot --;
                     if (IndexSlot < 0) IndexSlot = 3;
 
                     if (slotsSelectors[i].indexOfSlot < equipArea.equipSlots.Count && slotsSelectors[i].indexOfSlot >= 0)
                     {
                         equipArea.SetEquipSlot(IndexSlot);
                         onSelectSlot?.Invoke(IndexSlot);
                     }
                 }
avatar image
0

Answer by Derek_Brouwer · Jan 07, 2021 at 09:17 PM

Showing your code would make this a bit easier, but I'm assuming you are using an array or list to represent your inventory currently. You could make an int to hold the current spot in your inventory you have currently selected like so:

 int selectedItem = 0;

 void Update()
 {
     if(Input.GetButtonDown(psRt))
     {    
         selectedItem++;
 
         if(selectedItem > 3) selectedItem = 0;    
     }
     currentItem = inventory[selectedItem];
 }
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

120 People are following this question.

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

Related Questions

Singling out mutiple Character movement 3 Answers

setting a character controller orientation by script 1 Answer

Cannot Get PS3 Controller Input Working on Mac 0 Answers

How to map controls for any controller. 3 Answers

Trying to switch over to the new input system 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