Forum rules - please read before posting.

Camera Drag + Hotspot run on release

Greetings!
Need help with combining camera dragging and hotspot interaction on click release.

At this point i have GameCamera2dDragg prefab that works perfect.
But when i start to drag from hotspot area it runs hotspot instead — so i turned to Custom Script as interaction method and found few similar cases to get script from and place it in scene:
case1, case2 and case3.
...and unfortunately none of this scripts worked for me — hotspots wont run on any click down or release.

Is there any ideas what am i doing wrong?
May be the script in those cases is not complete (and i can't see it since im noob at scripting) ?

AC 1.77.3
U 2021.3.14

«1

Comments

  • edited July 2023

    The script in this thread is for a 3D game. An equivalent for 2D would be:

    using AC;
    using UnityEngine;
    
    public class ClickUpInteract2D : MonoBehaviour
    {
    
        private void OnEnable () { KickStarter.stateHandler.SetInteractionSystem (false); }
        private void OnDisable () { KickStarter.stateHandler.SetInteractionSystem (true); }
    
        private void Update ()
        {
            if ((Input.GetMouseButtonUp (0) || (Input.touchCount == 1 && Input.GetTouch (0).phase == TouchPhase.Ended)) && KickStarter.stateHandler.IsInGameplay () && KickStarter.playerInput.GetDragState () == DragState.None)
            {
                Ray ray = Camera.main.ScreenPointToRay (KickStarter.playerInput.GetMousePosition ());
                RaycastHit2D hit = UnityVersionHandler.Perform2DRaycast (
                            KickStarter.CameraMain.ScreenToWorldPoint (KickStarter.playerInput.GetMousePosition ()),
                            Vector2.zero,
                            KickStarter.settingsManager.hotspotRaycastLength,
                            1 << LayerMask.NameToLayer (KickStarter.settingsManager.hotspotLayer)
                            );
    
                if (hit.collider)
                {
                    Hotspot hitHotspot = hit.collider.gameObject.GetComponent<Hotspot> ();
                    if (hitHotspot)
                    {
                        hitHotspot.RunUseInteraction ();
                    }
                }
            }
        }
    
    
    }
    
  • Thank you a lot, Chris! <3
    Your script works perfect for my needs.

    Found few sideeffects though... might be helpful for someone else situation.
    1— it blocks inventory since you cant drop item.
    2— if hotspot label is visible, it will hold it visible after appearance

  • edited July 2023

    I beg your pardon, seems like i should ask the same question from the same past thread:
    With this script there is no drag if i start from hotspot area.
    You've posted a solution script there at the end — could you update it for 2d game too?

    PS
    Also, Is there an easy way to deactivate script on some scenes?
    If there is no script found in any scene or i deactivate it with animation during gameplay it cause interaction block.

  • You've posted a solution script there at the end — could you update it for 2d game too?

    The script above is the 2D equivalent of the ClickUpInteract script at the end of that thread.

    To allow for dragging while over a Hotspot, try this: open up AC's PlayerInput script and find the following (around line 2052 in the latest release):

    !KickStarter.stateHandler.IsInGameplay () ||
    

    Replace this with:

    !KickStarter.stateHandler.CanInteract () || !KickStarter.stateHandler.IsInGameplay () ||
    

    Does that give you the intended behaviour?

    If there is no script found in any scene or i deactivate it with animation during gameplay it cause interaction block.

    I've amended the above script to re-enable the Interaction system when it is disabled.

  • First of all thank you again for you carefull support!
    I did as you said — great at first sight!

    Then tested everything with touch screen on ios …
    and there is an issue (with ClickUpInteract2D script??):
    — on ios — when scene(with this script) starts and after each camera dragging no interaction is possible with the first tap — you have to tap anywhere once — then interaction possible on next taps.

    — on Pc it works stable — but gives me error on start at this scene and sometimes on quit :
    NullReferenceException: Object reference not set to an instance of an object ClickUpInteract2D.OnDisable () (at Assets/Scripts/ClickUpInteract2D.cs:8)

    Any ideas , is it script or is it me done smth wrong?

  • Hard to say - where are you placing the script, and are you manually enabling/disabling it?

    Here's a variant that shouldn't give a Console error, but the behaviour is slightly different:

    using AC;
    using UnityEngine;
    
    public class ClickUpInteract2D : MonoBehaviour
    {
    
        private void Update ()
        {
            if (KickStarter.stateHandler.CanInteract ()) return;
    
            if ((Input.GetMouseButtonUp (0) || (Input.touchCount == 1 && Input.GetTouch (0).phase == TouchPhase.Ended)) && KickStarter.stateHandler.IsInGameplay () && KickStarter.playerInput.GetDragState () == DragState.None)
            {
                Ray ray = Camera.main.ScreenPointToRay (KickStarter.playerInput.GetMousePosition ());
                RaycastHit2D hit = UnityVersionHandler.Perform2DRaycast (
                            KickStarter.CameraMain.ScreenToWorldPoint (KickStarter.playerInput.GetMousePosition ()),
                            Vector2.zero,
                            KickStarter.settingsManager.hotspotRaycastLength,
                            1 << LayerMask.NameToLayer (KickStarter.settingsManager.hotspotLayer)
                            );
    
                if (hit.collider)
                {
                    Hotspot hitHotspot = hit.collider.gameObject.GetComponent<Hotspot> ();
                    if (hitHotspot)
                    {
                        hitHotspot.RunUseInteraction ();
                    }
                }
            }
        }
    
    }
    

    With this one, you'll need to manually enable/disable the Interaction system using the Engine: Manage systems Action. When the Interaction system is disabled, the script will kick in.

  • edited July 2023

    The console error is gone, thanks to your update.

    My touch screen problem still remains, unfortunately.
    Funny thing is that I have also tested builds on android devices — and found out that it works! But not always correct. I still get many "not interacting first tap/touch" only after camera drag.

    Looks like percent of "blanc taps" after drag could be decreased with camera2ddrag settings. Best results i could get is on Android — i get like 7 good taps out of 10. On iOs its much worse. I had to rise Deceleration up to 10000 to get any noticeable change on ios (1/10). Is there a number for instant stop dragging?

    The problem somewhere between combination of camera2ddrag + clickUpInteract2d script — since it happens only when they are together. Maybe camera doesn't stop at the end of some drags and slightly moves, so system takes the following tap as an end of drag and interacts only on second tap.

  • What platforms are you looking to build to, and what is your game's "Interaction method" set to?

    A new feature planned for the v1.78 update is the option to have Hotspot react when releasing a tap, as opposed to starting a tap. With this option enabled, cameras will still be draggable when tapping down, even if the tap occurs over a Hotspot. It may be that this feature is the better approach, rather than involving a custom script.

  • edited July 2023

    Interaction method is Context Sensitive.
    The script attached to an empty game object and Interaction system manually disabled on scene start using the Engine: Manage systems.

    I'll build for touchscreen devices iOS and Android... possibly remaster for PC.

    Good to know with next AC update it will be easier!

  • From what you describe, it sounds like the update will be the best way to handle this - look out for it in the next week or so.

  • Greetings!
    After recent 1.78 update i've tested new 'Touch Up' input mode for interactions along with GameCamera2dDrag — unfortunately still face some no response taps/clicks, mostly right after drag, mostly on touchscreen (ios/android). Hopefully some improvement for that mode could be done in future.

    If somebody have a a stable working 'Touch Up' interactions — please share your settings — I will look for mistakes on my side.

    And it appeared to be more handy using Chris's custom script from above and manually disable Interaction system if I need 'Touch up' method only in few scenes.

  • unfortunately still face some no response taps/clicks, mostly right after drag, mostly on touchscreen (ios/android).

    If you can provide exact steps to recreate your issues, share them and I can look into it.

    And it appeared to be more handy using Chris's custom script from above and manually disable Interaction system if I need 'Touch up' method only in few scenes.

    Any Manager field can be modified through script at runtime - it would be possible to enable/disable the "Touch up" option as you enter given scenes.

    A tutorial on modifying Manager fields at runtime can be found here.

  • To recreate:
    — I started a new 2d project from scratch (U2021.3.14, AC1.78).
    — An empty scene with one hotspot that runs a simple animation.
    Settings manager: https://i.imgur.com/jvkZsWx.jpg
    — GameCamera2ddrag https://i.imgur.com/khQIbE6.png
    — then build to IOS/Android

    The problem happens only with TouchUp mode.
    And after closer look it has two problems in one.
    Im not sure if they connected to AC, but i hope there is an easy solution for both, cuz im stuck:

    1 — Camera Drag Problem — Hotspot won’t run on first touch after Camera Drag. Different behavior on platforms:
    On IOS — problem all the time.
    Android — problem sometimes.
    PC — problem sometime when I use laptop touchpad, and never with the mouse.

    2 — Tap Problem — TouchUp mode doesn’t respond on quick single tap on any device.

    By the way Double tap works perfect anytime everywhere.

  • edited August 2023

    Thanks for these - I'll follow them and investigate.

    And it appeared to be more handy using Chris's custom script from above and manually disable Interaction system if I need 'Touch up' method only in few scenes.

    To be clear: what are the remaining issue(s) if you use the script instead?

  • edited August 2023

    Yeep. If i use script — there's no tap Tap problem — I can activate hotspot using quick taps.
    CameraDrag problem remains.

  • The "Camera drag" behaviour is by design - so as to avoid inadvertent interactions when the player is dragging the camera.

    If you use the script, however, you can alter this behaviour by removing the && KickStarter.playerInput.GetDragState () == DragState.None portion.

  • edited August 2023

    I understand that behaviour, and it works well with the mouse. But with the touchscreen it somehow avoids intended interaction with the first touch after dragging the camera, only the second.

  • Sorry, I'm not clear on your wording. Could you rephrase, and have you tried altering the script in the way I mentioned above?

    Try also setting a small, but non-zero, value for the Settings Manager's Drag threshold value. This determines how far a click/touch must move before dragging effects kick in - it may need raising to be a little less sensitive when on touchscreens.

  • edited August 2023

    Sorry for the confusion.
    I was talking about "Camera drag" behaviour — that it works correct as designed — no inadvertent interactions. The only side effect appears on touchscreen devices —  after camera dragging, if player clicks to interact with the hotspot — nothing happens. So he have to click again to get it work.

    If you use the script, however, you can alter this behaviour by removing the && KickStarter.playerInput.GetDragState () == DragState.None portion.

    I removed the portion you suggested from the ClickUpInteract2D script — after that, inadvertent interactions became possible if I end the camera drag over hotspot collider and release touch.

    Setting Drag threshold value doesn't give any significant change, unfortunately.

  • edited August 2023

    I removed the portion you suggested from the ClickUpInteract2D script — after that, inadvertent interactions became possible if I end the camera drag over hotspot collider and release touch.

    As I was reading it, that was the intended behaviour - apologies for the misunderstanding.

    Does this alternative give better results?

    using AC;
    using UnityEngine;
    
    public class ClickUpInteract2D : MonoBehaviour
    {
    
        private void Update ()
        {
            if (KickStarter.stateHandler.CanInteract ()) return;
    
            if (Input.touchCount == 1 && Input.GetTouch (0).phase == TouchPhase.Ended && KickStarter.stateHandler.IsInGameplay () && KickStarter.playerInput.GetDragState () == DragState.None)
            {
                #if UNITY_EDITOR || UNITY_STANDALONE
                Vector2 position = Input.mousePosition;
                #else
                Vector2 position = Input.GetTouch (0).position;
                #endif
                Ray ray = Camera.main.ScreenPointToRay (position);
                RaycastHit2D hit = UnityVersionHandler.Perform2DRaycast (
                    KickStarter.CameraMain.ScreenToWorldPoint (position),
                    Vector2.zero,
                    KickStarter.settingsManager.hotspotRaycastLength,
                    1 << LayerMask.NameToLayer (KickStarter.settingsManager.hotspotLayer)
                );
    
                if (hit.collider)
                {
                    Hotspot hitHotspot = hit.collider.gameObject.GetComponent<Hotspot> ();
                    if (hitHotspot)
                    {
                        hitHotspot.RunUseInteraction ();
                    }
                }
            }
        }
    
    }
    
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.