Moving the camera from an OverlapPoint

Hello, please tell me, I have such a problem that I can’t solve for the second day.
I’m making a 2D game, the movement takes place by selecting one of several characters on the stage and clicking on the “active object”, I implemented it all by searching for a certain layer (OverlapPoint), when the click occurs, and the layer is correct, the animation starts and the character moves to the point when the click does not occur on the “active object”, and for any other area, the character and animation stops.
In the course of development, an unpleasant thing turned out:
When I moved the camera with the mouse button pressed, when I pressed the button to move the camera, the character, for the above reasons, simply stops and stops moving to the point, but must continue moving further.
Can you please tell me what can be done in this situation? maybe try to implement the camera movement in a different way ? or something else.

The main script:

public class GameManger : MonoBehaviour
{
    [SerializeField] private LayerMask[] LayerMasks;

    [SerializeField] private Camera Camera;

    [SerializeField] private float SpeedPersonage;

    private GameObject Personage;

    private PersonageSettings PersonageSettings;

    private void Update()
    {
        HighlightingPersonage();
        MovingToItems(Personage);
    }

    ////checking the layer.
    private Collider2D PersonageSelection(LayerMask LayerMask)
    {
        Vector3 MousePosition = Camera.ScreenToWorldPoint(Input.mousePosition);
        Collider2D hit = Physics2D.OverlapPoint(MousePosition, LayerMask);

        return hit;
    }

//Adding a character to the inspector.
    private void HighlightingPersonage()
    {
        var PersonageSelect = PersonageSelection(LayerMasks[0]);

        if (Input.GetMouseButtonDown(0))
        {
            if (PersonageSelect)
            {
                Personage = PersonageSelect.gameObject;
                PersonageSettings = Personage.GetComponent<PersonageSettings>();
            }
        }
    }

    ////Character rotation.
    private void PersonageRotate(Collider2D TargetRotate)
    {
        if(Personage && TargetRotate)
        {
            float Position = TargetRotate.transform.position.x;

            if (Personage.transform.position.x > Position)
                Personage.GetComponent<SpriteRenderer>().flipX = false;
            else if (Personage.transform.position.x < Position)
                Personage.GetComponent<SpriteRenderer>().flipX = true;
        }
    }

    ////The movement of the object is set.
    private bool Moving(GameObject Personage, Collider2D TargetMove)
    {
        bool Move = false;

        if (Personage)
        {
            if (TargetMove && Personage.transform.position.x != TargetMove.transform.position.x)
            {
                var Speed = SpeedPersonage * Time.deltaTime;
                Move = true;

                Personage.transform.position = Vector2.MoveTowards(Personage.transform.position,
                    new Vector2(TargetMove.transform.position.x, Personage.transform.position.y), Speed);
            }
            else Move = false;
        }

        return Move;
    }

    //Start and stop animation.
    private void AnimationPersonage(GameObject Personage,Collider2D Target,string NameAnimation)
    {
        var MovePersonage = Moving(Personage, Target);

        if (Personage)
        {
            if (Target)
            {
                if (MovePersonage == true)
                    PersonageSettings.AnimationMovePersonage.SetBool(NameAnimation, true);
                else PersonageSettings.AnimationMovePersonage.SetBool(NameAnimation, false);
            }
            else PersonageSettings.AnimationMovePersonage.SetBool(NameAnimation, false);
        }
    }

    //Call to move and rotate to the object.
    private void MovingToItems(GameObject Personage)
    {
        Collider2D Target = PersonageSelection(LayerMasks[1]);

        PersonageRotate(Target);
        Moving(Personage, Target);
        AnimationPersonage(Personage, Target, "Move");
    }
}

The code for moving the camera, here I tried to do something with the bool variable, but it didn’t work out.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveCamera : MonoBehaviour
{
    private Vector2 StartPositionCamera;

    [SerializeField] internal Vector3 Old;
    [SerializeField] internal Vector3 Position;

    [SerializeField] internal bool CameraMove = false;

    void Update()
    {
        Move();
    }

    private bool Move()
    {
        if (Input.GetMouseButtonDown(0))
        {
            StartPositionCamera = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Old = transform.position;
        }
        else if(Input.GetMouseButtonUp(0))
            Old = transform.position;

        if (Input.GetMouseButton(0))
        {
            float NewPositionCamera = Camera.main.ScreenToWorldPoint(Input.mousePosition).x - StartPositionCamera.x;
            Position = transform.position = new Vector3(transform.position.x - NewPositionCamera, transform.position.y, transform.position.z);
        }

        if (Position == Old)
        {
            CameraMove = false;
        }
        else CameraMove = true;

        return CameraMove;
    }
}

For clarity of the video:
helpfulreadyassassinbug

You could always just check whether you’re holding down the Input by adding a delay check to see how long they are, and only change the “Target” if it’s less than a certain amount, eg. checking whether they tap or drag.

I also think gameplay wise it’s kind of weird that you check for a target every frame even when there’s no input.

1 Like

im gonna need some help understanding this comment

youve lost me there

1 Like

From what I guessed, it means “When I move the camera, I want the player to keep moving, but it stops because of my movement script”

1 Like

Thank you, I like this solution to the problem.

Translators don’t translate perfectly :slight_smile: