Forum rules - please read before posting.

Player 2D movement and panning

edited February 5 in Technical Q&A

I use panning via Camera Drag 2D, but whenever I "grab" the scene to scroll around it the player follows to the position where I grabbed it. I would like the player to stay put while I use panning and move only based on a simple click/tap when the panning is not used. I use the point and click movement method (it is a pnc game).

I think the solution could be that the player walks to a destination only after:
1) mouse up/tap has been released
2) between mouse down/tap down, there was 0 drag detected
Otherwise, only panning would occur.

How could I implement this modification to player movement input?

Comments

  • *2) between mouse down/tap down AND mouse up/tap release, there was 0 drag detected

  • You can achieve this with the use of input override delegates - see the Manual's "Remapping inputs" chapter for details.

    This might need more tweaking to get working for other devices / account for Menus etc, but something along these lines should do it:

    using UnityEngine;
    using AC;
    
    public class ClickUpToMove : MonoBehaviour
    {
    
        Vector3 startPosition;
    
        private void Start ()
        {
            KickStarter.playerInput.InputGetMouseButtonDownDelegate = MyGetMouseButtonDown;
            KickStarter.playerInput.InputGetMouseButtonDelegate = MyGetMouseButton;
        }
    
        private bool MyGetMouseButtonDown (int button)
        {
            if (button == 0 && Input.GetMouseButtonDown (0))
            {
                startPosition = Input.mousePosition;
                return false;
            }
            return Input.GetMouseButtonUp (button);
        }
    
        private bool MyGetMouseButton (int button)
        {
            if (button == 0 && Input.GetMouseButton (0) && Input.mousePosition != startPosition)
            {
                return true;
            }
            return false;
        }
    
    }
    
  • Works great so far, thanks! Will report how it works with the menus and inventory once i get to that bit :-)

  • So unfortunately it only works when using the mouse. But it doesn't work on mobile while using the touchscreen - which is actually where I will be needing it the most. Can you please help?

  • This would be the equivalent for touch input:

    using UnityEngine;
    using AC;
    
    public class ClickUpToMove : MonoBehaviour
    {
    
        Vector3 startPosition;
    
        private void Start ()
        {
            KickStarter.playerInput.InputGetMouseButtonDownDelegate = MyGetMouseButtonDown;
            KickStarter.playerInput.InputGetMouseButtonDelegate = MyGetMouseButton;
        }
    
        private bool MyGetMouseButtonDown (int button)
        {
            if (button == 0 && Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began)
            {
                startPosition = Input.GetTouch (0).position;
                return false;
            }
            return Input.touchCount > 0 ? Input.GetTouch (button).phase == TouchPhase.Ended : false;
        }
    
        private bool MyGetMouseButton (int button)
        {
            if (button == 0 && Input.touchCount > 0 && Input.GetTouch (0).position != startPosition)
            {
                return true;
            }
            return false;
        }
    
    }
    

    We can merge them together if successful, but it needs the Input method to remain as Mouse And Keyboard for it to kick in.

  • edited February 7

    It is giving me compiler error CS0034 regarding the operator != not being specific on Vector2 and 3 on this line:
    if (button == 0 && Input.touchCount > 0 && Input.GetTouch(0).position != startPosition)

  • Replace Vector3 with Vector2.

  • The character does not move or react at all. I tried to set mouse and keyboard and touch screen and the result is still the same.

  • edited February 7

    I tried the script from February 6 on the mobile version while setting mouse and keyboard input. It works nicely, however, I am also using pinch zoom and once I attempt to zoom in, the game goes bonkers and jumps outside of the scene into black. That is a problem, as I need to be able to zoom, but also i want to be able to scroll around the scene without the character running after my finger. Is it even possible to use the pinch zoom when the mouse and key are set as input?

  • I'll need to be very clear on your exact setup. Can you share the scripts involved, with details on where they're attached / screenshots of your Settings Manager?

  • Sure. Currently, I am using Cmera2DDrag with this Zoom script attached to it:

    using UnityEngine;
    
    public class PanZoom : MonoBehaviour
    {
        Vector3 touchStart;
        public AC._Camera gameCamera;
        public float zoomOutMin = 1;
        private float maxOrthographicSize;
        public float zoomSpeed = 10f;
    
        void Start()
        {
            maxOrthographicSize = gameCamera.Camera.orthographicSize;
        }
    
        void Update()
        {
            if (Input.touchCount == 2)
            {
                Touch touchZero = Input.GetTouch(0);
                Touch touchOne = Input.GetTouch(1);
    
                Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
                Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
    
                float prevMagnitude = (touchZeroPrevPos - touchOnePrevPos).magnitude;
                float currentMagnitude = (touchZero.position - touchOne.position).magnitude;
    
                float difference = currentMagnitude - prevMagnitude;
    
                Zoom(difference * 0.01f); // Adjusted zoom factor for touch input
            }
            else
            {
                Zoom(Input.GetAxis("Mouse ScrollWheel"));
            }
        }
    
        void Zoom(float increment)
        {
            float newOrthographicSize = gameCamera.Camera.orthographicSize - increment * zoomSpeed;
    
            // Clamp the new orthographic size within the specified range and the maximum orthographic size for the scene
            gameCamera.Camera.orthographicSize = Mathf.Clamp(newOrthographicSize, zoomOutMin, maxOrthographicSize);
        }
    }
    

    Then I am using your ClickUpToMove script attached to an empty object in each scene. When I use the version from 6. Feb, the camera flickers and ultimately flies away from the scene while zooming. When I use the version from Feb 7, the character does not react to clicking and does not move at all.

    Lastly, I am using frame flip script on the Player (but I assume that does not affect the issue?) https://adventure-creator.fandom.com/wiki/Frame-flipping_for_Sprites_Unity_Complex_characters

  • The Feb 7 version references touch-screen input - are you testing on a device, or in the Editor?

  • My mistake, trying on an Android device now but it is also not good. The character is moving this time, however, panning does not work, when I tap somewhere while the character is moving, it twitches (like the movement command was reset, it does not do that in other instances). Also when I try to zoom in, the camera starts to shiver and then flies away completely outside of the scene into black.

  • edited February 10

    As this needs to also consider the zooming and custom camera motion in your other thread, let's get the latter working first in the Editor with the mouse - and revisit Android once its behaving on a single platform first.

    Combined with the other thread, there are now three custom scripts at play. I've updated your other thread - disable PanZoom for now, let's get the other working and we can then add the extra functionality.

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.