Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Moving the camera from an OverlapPoint

Discussion in 'Scripting' started by Slabada, May 16, 2022.

  1. Slabada

    Slabada

    Joined:
    May 2, 2021
    Posts:
    50
    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:
    Code (CSharp):
    1. public class GameManger : MonoBehaviour
    2. {
    3.     [SerializeField] private LayerMask[] LayerMasks;
    4.  
    5.     [SerializeField] private Camera Camera;
    6.  
    7.     [SerializeField] private float SpeedPersonage;
    8.  
    9.     private GameObject Personage;
    10.  
    11.     private PersonageSettings PersonageSettings;
    12.  
    13.     private void Update()
    14.     {
    15.         HighlightingPersonage();
    16.         MovingToItems(Personage);
    17.     }
    18.  
    19.     ////checking the layer.
    20.     private Collider2D PersonageSelection(LayerMask LayerMask)
    21.     {
    22.         Vector3 MousePosition = Camera.ScreenToWorldPoint(Input.mousePosition);
    23.         Collider2D hit = Physics2D.OverlapPoint(MousePosition, LayerMask);
    24.  
    25.         return hit;
    26.     }
    27.  
    28. //Adding a character to the inspector.
    29.     private void HighlightingPersonage()
    30.     {
    31.         var PersonageSelect = PersonageSelection(LayerMasks[0]);
    32.  
    33.         if (Input.GetMouseButtonDown(0))
    34.         {
    35.             if (PersonageSelect)
    36.             {
    37.                 Personage = PersonageSelect.gameObject;
    38.                 PersonageSettings = Personage.GetComponent<PersonageSettings>();
    39.             }
    40.         }
    41.     }
    42.  
    43.     ////Character rotation.
    44.     private void PersonageRotate(Collider2D TargetRotate)
    45.     {
    46.         if(Personage && TargetRotate)
    47.         {
    48.             float Position = TargetRotate.transform.position.x;
    49.  
    50.             if (Personage.transform.position.x > Position)
    51.                 Personage.GetComponent<SpriteRenderer>().flipX = false;
    52.             else if (Personage.transform.position.x < Position)
    53.                 Personage.GetComponent<SpriteRenderer>().flipX = true;
    54.         }
    55.     }
    56.  
    57.     ////The movement of the object is set.
    58.     private bool Moving(GameObject Personage, Collider2D TargetMove)
    59.     {
    60.         bool Move = false;
    61.  
    62.         if (Personage)
    63.         {
    64.             if (TargetMove && Personage.transform.position.x != TargetMove.transform.position.x)
    65.             {
    66.                 var Speed = SpeedPersonage * Time.deltaTime;
    67.                 Move = true;
    68.  
    69.                 Personage.transform.position = Vector2.MoveTowards(Personage.transform.position,
    70.                     new Vector2(TargetMove.transform.position.x, Personage.transform.position.y), Speed);
    71.             }
    72.             else Move = false;
    73.         }
    74.  
    75.         return Move;
    76.     }
    77.  
    78.     //Start and stop animation.
    79.     private void AnimationPersonage(GameObject Personage,Collider2D Target,string NameAnimation)
    80.     {
    81.         var MovePersonage = Moving(Personage, Target);
    82.  
    83.         if (Personage)
    84.         {
    85.             if (Target)
    86.             {
    87.                 if (MovePersonage == true)
    88.                     PersonageSettings.AnimationMovePersonage.SetBool(NameAnimation, true);
    89.                 else PersonageSettings.AnimationMovePersonage.SetBool(NameAnimation, false);
    90.             }
    91.             else PersonageSettings.AnimationMovePersonage.SetBool(NameAnimation, false);
    92.         }
    93.     }
    94.  
    95.     //Call to move and rotate to the object.
    96.     private void MovingToItems(GameObject Personage)
    97.     {
    98.         Collider2D Target = PersonageSelection(LayerMasks[1]);
    99.  
    100.         PersonageRotate(Target);
    101.         Moving(Personage, Target);
    102.         AnimationPersonage(Personage, Target, "Move");
    103.     }
    104. }
    The code for moving the camera, here I tried to do something with the bool variable, but it didn't work out.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MoveCamera : MonoBehaviour
    6. {
    7.     private Vector2 StartPositionCamera;
    8.  
    9.     [SerializeField] internal Vector3 Old;
    10.     [SerializeField] internal Vector3 Position;
    11.  
    12.     [SerializeField] internal bool CameraMove = false;
    13.  
    14.     void Update()
    15.     {
    16.         Move();
    17.     }
    18.  
    19.     private bool Move()
    20.     {
    21.         if (Input.GetMouseButtonDown(0))
    22.         {
    23.             StartPositionCamera = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    24.             Old = transform.position;
    25.         }
    26.         else if(Input.GetMouseButtonUp(0))
    27.             Old = transform.position;
    28.  
    29.         if (Input.GetMouseButton(0))
    30.         {
    31.             float NewPositionCamera = Camera.main.ScreenToWorldPoint(Input.mousePosition).x - StartPositionCamera.x;
    32.             Position = transform.position = new Vector3(transform.position.x - NewPositionCamera, transform.position.y, transform.position.z);
    33.         }
    34.  
    35.         if (Position == Old)
    36.         {
    37.             CameraMove = false;
    38.         }
    39.         else CameraMove = true;
    40.  
    41.         return CameraMove;
    42.     }
    43. }
    44.  
    For clarity of the video:
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    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.
     
    Slabada likes this.
  3. SourGummi

    SourGummi

    Joined:
    Nov 14, 2021
    Posts:
    96
    im gonna need some help understanding this comment
    youve lost me there
     
    Slabada likes this.
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    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"
     
    Slabada likes this.
  5. Slabada

    Slabada

    Joined:
    May 2, 2021
    Posts:
    50
    Thank you, I like this solution to the problem.
     
  6. Slabada

    Slabada

    Joined:
    May 2, 2021
    Posts:
    50
    Translators don't translate perfectly :)