Forum rules - please read before posting.

Joystick simultaneous right/left and scrolling inventory box

Hi,

Thank you always for all the great updates!!

Joystick use
I am using the included touchscreen joystick in a first person game and would like to be able to use both the Player and Camera joysticks simultaneously. It seems right now I need to let go of e.g. the player movement joystick before I can use the camera/look-arond joystick and vice versa. What can I change to be able to use both at the same time, e.g. look around while walking forward having both pressed at the same time?

Scrolling inventory
I have a one row inventory box with infinite slots and 8 visible slots. Instead of using back and forth buttons on either side, I'd like to be able to scroll left and right with the finger on the touch screen to slide the visible 8 slots. The scrolling should stop when reaching the current amount of inventory items held. Optimally it will scroll smoothly, i.e. not shift one inventory item at the time.

Thank you anyone who can help me with this!

// Magnus

Comments

  • One more thing. How do I disable one of the buttons temporarily without disabling the whole joystick? I have tried to disable the game object that holds the button component, but this only hides the button.

  • What can I change to be able to use both at the same time, e.g. look around while walking forward having both pressed at the same time?

    This should be the default behaviour. What device/platform are you building to, and what is your project's "Active Input Handling" set to?

    Scrolling inventory

    A scrolling inventory can be achieved by creating all of the slots (not just the 8) in your UI prefab, referencing them in your Menu's InventoryBox element, and then using Unity UI's Scroll View to only show 8 at a time. I'm not sure about making "infinite", though - how many slots would you realistically need?

    How do I disable one of the buttons temporarily without disabling the whole joystick?

    Disabling the Button GameObject won't currently prevent input - but it should do. I will update the template with this behaviour, thanks for the report. In the meantime, you could try moving the button off-screen, but I'll see about updating the template shortly.

  • Thanks a lot for this!!!

    • Joystick both buttons:
      I'm building for iOS. Active input handling is set to "Both", although I tried the Old and the New as well with the same result. I am testing it through the unityRemote, so not sure if that can have anything to do with it.

    • Scrolling inventory
      Yes, works great with Scroll Rect component and Unity UI!
      Comment: The inventory items keep scrolling when dragging an item outside the "panel"-inventory box, although I solved this with

       public GameObject panel;
    
       // Update is called once per frame
       void Update()
       {
           if (KickStarter.runtimeInventory.SelectedItem != null)
           {
               Vector3 screenPosition = Input.mousePosition;
               if (screenPosition.y > 350)
               {
                   panel.GetComponent<ScrollRect>().enabled = false;
               }
    
           }
           else
           {
               panel.GetComponent<ScrollRect>().enabled = true;
           }
       }
    

    Works ok, but not sure what happens with screens of different resolutions?

    • Button disable
      Ok, understood

    • Joystick new question. I've played around with the Joystick UI script and tried to add the below, but haven't managed to make it work

    1. Double click on the Player joystick (holding it down after the second click) to Run
    2. Using a third person camera for 3D closeups, either
      a. Redifining the player joystick to zoom in and out (spinning the view with the Camera joystick works fine)
      b. disabling the player joystic, repositioning the camera joystick in the middle and adding a pinch zoom in and out to the same "button".
  • I am testing it through the unityRemote, so not sure if that can have anything to do with it.

    When using Input System, you'll need to have Use Enhanced Touch Support checked in your Controls Reader prefab, but it should work fine when using the older Input System.

    However, multi-touch not working with Unity Remote is not supported IIRC.

    Works ok, but not sure what happens with screens of different resolutions?

    You might be able to instead check for whether or not the mouse is currently over the Menu or not, which you can do with:

    if (KickStarter.playerMenus.MouseOverMenu != null && KickStarter.playerMenus.MouseOverMenu.title == "Inventory")
    { }
    

    Disabling the Button GameObject won't currently prevent input - but it should do.

    This is now the case - I've updated the template.

    Double click on the Player joystick (holding it down after the second click) to Run

    You could hook into the Joystick's OnJoystickStartMoving / StopMoving events to record the gap in time between releasing and re-controlling, and force the Player to run/walk, but you can't currently distinguish between the two provided joysticks. I'll look into this shortly.

    disabling the player joystic, repositioning the camera joystick in the middle and adding a pinch zoom in and out to the same "button".

    I'll also see if it's possible to configure multiple joysticks, or affect the inputs used by them through external script.

  • Thanks a lot again

    • Where can I find the Controls Reader prefab? I don't seem to have it.

    • Mouse over menu.
      This worked!:

        public GameObject panel;
        bool overMenu;
    
        void Update()
        {
            overMenu = KickStarter.playerMenus.IsMouseOverInventory();
                if (KickStarter.runtimeInventory.SelectedItem != null && !overMenu)
                {
                    panel.GetComponent<ScrollRect>().enabled = false;
                }           
                else
                {
                panel.GetComponent<ScrollRect>().enabled = true;
                } 
        }
    }
    
    • I downloaded the (new?) mobile joystick package from the downloads page, but I get errors when importing:
      https://imgur.com/a/vKhdxE0

    I will try the Onjoystickstart moving, but with my skills I may wait for another solution... :-)

    Thanks a lot!

  • Where can I find the Controls Reader prefab? I don't seem to have it.

    It's part of AC's Input System integration package, which I recommend using if you want to switch over to Input System. It's available over on the Downloads page.

    If you weren't using it, though, it's likely the issue is down to the limitation of Unity Remote.

    I downloaded the (new?) mobile joystick package from the downloads page, but I get errors when importing:

    It requires the latest version of AC to be installed.

  • Ok, great thanks. AC updated!

    When importing the Input System integration package I get loads of errors though. E.g. Assets\AdventureCreator\Downloads\Input System integration\Scripts\ACCursorPositionProcessor.cs(4,19): error CS0234: The type or namespace name 'InputSystem' does not exist in the namespace 'UnityEngine' (are you missing an assembly reference?)

    All the errors refer to "The type or namespace name 'XXX' does not exist in the namespace 'UnityEngine'

    I am using Unity 2021.3.8f1

  • I also got these errors when updating AC. Did I miss importing something?
    https://imgur.com/a/UK4dcFf

  • edited January 24

    Do you have Unity's Input System package already installed from the Package Manager?

  • No I didn't. Now I have and the Input system errors are gone. I still have the ActionCameraCheck ones though.

  • Sorry to mix different things, but they all happened at the same time so maybe related. I get these messages "The associated script cannot be loaded" for both the Input integration and the Mobile Joystick.
    https://imgur.com/pCfn8rF

  • Hi, I all the above solved. I noticed I had a few scripts "e.g. ActionCameraCheck" in two places so when deleting the wrong ones all turned out good. Thanks again for all the help!

  • You could hook into the Joystick's OnJoystickStartMoving / StopMoving events to record the gap in time between releasing and re-controlling, and force the Player to run/walk, but you can't currently distinguish between the two provided joysticks.
    

    First time ever to try code with events... I've done the below hoping that the player (first person game) will start running when the gap is below a set threshold, but nothing happens.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using AC;
    using AC.Downloads.MobileJoystick;
    
    public class JoystickRun : MonoBehaviour
    {
        public float pressDelta;
        public float runThreshold;
        public float tempdelta;
        public bool isRunning;
        public float currentSpeed;
    
        private void OnEnable ()
        { 
            Joystick.OnJoystickStopMoving += OnJoystickStopMoving;
            Joystick.OnJoystickStartMoving += OnJoystickStartMoving;
        }
        private void OnDisable () 
        { 
            Joystick.OnJoystickStopMoving -= OnJoystickStopMoving;
            Joystick.OnJoystickStartMoving -= OnJoystickStartMoving;
        }
    
        private void OnJoystickStopMoving(Joystick myJoystick)
        {
            pressDelta = 0f;
        }
    
        private void OnJoystickStartMoving(Joystick myJoystick)
        {
            if (pressDelta < runThreshold)
            {
                tempdelta = pressDelta;
                KickStarter.player.isRunning = true;
                isRunning = KickStarter.player.isRunning;
            }
            else
            {
                tempdelta = pressDelta;
                KickStarter.player.isRunning = false;
                isRunning = KickStarter.player.isRunning;
            }
        }
    
        private void Update()
        {
            pressDelta++;
            currentSpeed = KickStarter.player.GetMoveSpeed();
        }
    }
    

    I also tried to simulate my run Input Button "Run" with

    KickStarter.playerInput.SimulateInputButton ("Run");
    

    instead of the isRunning = true, but no change there either. The player does run when pressing the assigned keyboard button though. I thought about just changing speed, but I'd like to keep the states of walk and run to attach sounds.

  • edited February 11

    To have the effect only kick in when using the Player joystick, and not the Camera, you need to compare the event variable with that of the JoystickUI component.

    You don't need to increase any timer in an Update loop - you can just use Time.time to record the time of the last touch.

    Rather than trying to simulate the Run button, you're better off locking the Player's run state to either always walking, or running:

    using UnityEngine;
    using AC;
    using AC.Downloads.MobileJoystick;
    
    public class JoystickRun : MonoBehaviour
    {
    
        public JoystickUI joystickUI;
        public float runThreshold = 0.2f;
        float timeOfLastStart;
    
        private void OnEnable () { Joystick.OnJoystickStartMoving += OnJoystickStartMoving; }
        private void OnDisable () { Joystick.OnJoystickStartMoving -= OnJoystickStartMoving; }
    
        private void OnJoystickStartMoving (Joystick joystick)
        {
            if (joystickUI == null || joystick == joystickUI.PlayerJoystick)
            {
                bool isRunning = timeOfLastStart + runThreshold > Time.time;
                KickStarter.player.runningLocked = isRunning ? PlayerMoveLock.AlwaysRun : PlayerMoveLock.AlwaysWalk;
                timeOfLastStart = Time.time;
            }
        }
    
    }
    
  • Thank you so much for this. It works great!
    I had to change the "joystickUI.playerJoystick" from private to public in the JoystickUI script and it works like a charm!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.