Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[SOLVED]Movement by clicking

Discussion in '2D' started by Franchiscue, Mar 25, 2017.

  1. Franchiscue

    Franchiscue

    Joined:
    Mar 24, 2017
    Posts:
    1
    Hello guys, I was trying to make a tactical RPG similar to Fire Emblem and I get problems when it comes to move the units. My idea is that you click on a unit and then make another click to move it to the destination. I have 2 colliders: a BoxCollider2D (called bc2D) and a CircleCollider2D (called cc2D) that limits the distance the unit can move, but I can't make it move. So here's my code:

    Code (CSharp):
    1. void MoveThere()
    2.     {
    3.         if (Input.GetMouseButtonDown(0))
    4.         {
    5.            
    6.             Physics2D.queriesHitTriggers = false;
    7.             Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    8.             RaycastHit2D hit = Physics2D.Raycast(worldPoint, Vector2.zero);
    9.             if (hit.collider != null && hit.collider == bc2D)
    10.             {
    11.                
    12.                 Physics2D.queriesHitTriggers = true;
    13.                 firstPosition = gameObject.transform.position;
    14.                 SetClickedOnPlayer(true);
    15.                
    16.                 if (GetClickedOnPlayer() && Input.GetMouseButtonDown(1) && hit.collider != null && hit.collider == cc2D)
    17.                 {
    18.                    
    19.                     worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    20.                     hit = Physics2D.Raycast(worldPoint, Vector2.zero);
    21.                     secondPosition = hit.point;
    22.                     this.gameObject.transform.position = secondPosition;
    23.                     SetClickedOnPlayer(false);
    24.                 }
    25.                 else
    26.                 {
    27.                     this.gameObject.transform.position = firstPosition;
    28.                 }
    29.             }
    30.         }
    31.     }
    I'm pretty sure it's just something very stupid I can't see, but I'll be grateful if anyone can help me.