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

[Help][2D] Move to the closest region

Discussion in 'Scripting' started by Impacto, Apr 12, 2014.

  1. Impacto

    Impacto

    Joined:
    Apr 12, 2014
    Posts:
    3
    Hello everybody.

    I'm getting started with C# from C++, so I'm pretty sure that my problems are sintaxis-wise.

    The point of this script is that whenever I click over somewhere in the map, it calculates the closest region compared with the position of the click and move an object towards there.

    There are 4 regions (made this with easy numbers so I could test it more efficiently):
    Region 1: (-1, 1)
    Region 2: (1, 1)
    Region 3: (-1, -1)
    Region 4: (1, -1)

    In the code, I simulated a Vector that started in the click position and ended in one of those regions (switching between the regions with a For sentence and calculating the longitude of the whole vector; the vector with the lowest distance would be the one of the correct region to move)

    Here's the code:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4.  
    5.  
    6. using System.Collections;
    7.  
    8.  
    9.  
    10. public class Move : MonoBehaviour
    11.  
    12. {
    13.  
    14.  
    15.  
    16.     public Vector3 closestCountry(Vector3 punto)
    17.  
    18.     {
    19.  
    20.  
    21.  
    22. /*
    23.  
    24. 1: (-1, 1)
    25.  
    26. 2: (1, 1)
    27.  
    28. 3: (-1, -1)
    29.  
    30. 4: (1, -1)
    31.  
    32.  */
    33.  
    34.         Vector3 result;
    35.  
    36.         result.x = punto.x;
    37.  
    38.         result.y = punto.y;
    39.  
    40.         result.z = 0;
    41.  
    42.  
    43.  
    44.         Vector3 region;
    45.  
    46.         region.x = 0;
    47.  
    48.         region.y = 0;
    49.  
    50.         region.z = 0;
    51.  
    52.         float dist = 100;
    53.  
    54.  
    55.  
    56.         for (int i=1; i<=4; i++)
    57.  
    58.         {
    59.  
    60.  
    61.  
    62.             if(i==1)
    63.  
    64.             {
    65.  
    66.                 region.x = -1;
    67.  
    68.                 region.y = 1;
    69.  
    70.                 region.z = 0;
    71.  
    72.             }
    73.  
    74.             if(i==2)
    75.  
    76.             {
    77.  
    78.                 region.x = 1;
    79.  
    80.                 region.y = 1;
    81.  
    82.                 region.z = 0;
    83.  
    84.             }
    85.  
    86.             if(i==3)
    87.  
    88.             {
    89.  
    90.                 region.x = -1;
    91.  
    92.                 region.y = -1;
    93.  
    94.                 region.z = 0;
    95.  
    96.             }
    97.  
    98.             if(i==4)
    99.  
    100.             {
    101.  
    102.                 region.x = 1;
    103.  
    104.                 region.y = -1;
    105.  
    106.                 region.z = 0;
    107.  
    108.             }
    109.  
    110.  
    111.  
    112.             if(dist>=(Mathf.Sqrt(Mathf.Pow((punto.x - region.x), 2) + Mathf.Pow((punto.y - region.y), 2))))
    113.  
    114.             {
    115.  
    116.                 dist = (Mathf.Sqrt(Mathf.Pow((punto.x - region.x), 2) + Mathf.Pow((punto.y - region.y), 2)));
    117.  
    118.                 result.x = region.x;
    119.  
    120.                 result.y = region.y;
    121.  
    122.                 result.z = 0;
    123.  
    124.             }
    125.  
    126.         }
    127.  
    128.         return result;
    129.  
    130. }
    131.  
    132.  
    133.  
    134.     void Start ()
    135.  
    136.     {
    137.  
    138.  
    139.  
    140.     }
    141.  
    142.    
    143.  
    144.     void Update ()
    145.  
    146.     {
    147.  
    148.  
    149.  
    150.         if(Input.GetMouseButtonDown(0))
    151.  
    152.         {
    153.  
    154.             var posClick = Input.mousePosition;
    155.  
    156.             Vector3 click;
    157.  
    158.             click.x = posClick.x;
    159.  
    160.             click.y = posClick.y;
    161.  
    162.             click.z = 10;
    163.  
    164.  
    165.  
    166.             transform.position = closestCountry(posClick);
    167.  
    168.         }
    169.  
    170.  
    171.  
    172.     }
    173.  
    174.  
    175.  
    176. }
    177.  
    When I start the program and click anywhere, the object moves to remote coords (like (854, 756) and stuff like that) when it shouldn't.
    I hope you guys could tell me where am I derping.

    Thanks!
     
  2. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    Why not just have one trigger collider for each of your regions and detect mouse presses on those?
     
  3. Impacto

    Impacto

    Joined:
    Apr 12, 2014
    Posts:
    3
    Can you tell me how to do that? I'm not familiar with Unity, I could use a good tutorial about that.

    Thanks!
     
  4. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    1. Create an empty game object (your region).
    2. Add a box collider.
    3. Mark collider as "Trigger" using the inspector.
    4. Attach a custom MonoBehaviour to this object which responds to the OnMouseDown message.
    5. Save as prefab so that you can replicate as needed.

    For example:
    Code (csharp):
    1.  
    2. // ClickableRegion.cs
    3. using UnityEngine;
    4.  
    5. public class ClickableRegion : MonoBehaviour {
    6.  
    7.     private void OnMouseDown() {
    8.         Debug.Log("Mouse button was pressed on region '" + name + "'.");
    9.     }
    10.  
    11. }
    12.  
    I hope that this helps :)