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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Click to move 2D

Discussion in '2D' started by jaekx, Feb 25, 2015.

  1. jaekx

    jaekx

    Joined:
    Dec 15, 2014
    Posts:
    27
    Essentially I want to make a 2d moba with click to move. So far it works like this....

    If I click to the right of the character he studder steps right, if i hold click with the cursor to the right of the character he walks until he reaches my cursors x position and vice versa for going left.. that all works and is fine and dandy, however, I want it to work slightly differently.

    What my goal is for the player to click (not hold click but just click) and have the player move from his current x position to the clicked spots x position. No holding of click or clicking a bunch...

    any help?

    thanks!

    public class clickToMove : MonoBehaviour
    {

    public float speed = 4f;

    // Use this for initialization
    void Start ()
    {

    }

    // Update is called once per frame
    void Update ()
    {
    Movement ();
    }

    void Movement()
    {
    Vector2 mp = Camera.main.ScreenToWorldPoint (Input.mousePosition);

    if (Input.GetMouseButton(1)&& mp.x > transform.position.x + .5)
    {
    transform.eulerAngles = new Vector2(0,0);
    transform.Translate(Vector2.right * speed * Time.deltaTime);
    }
    if (Input.GetMouseButton(1) && mp.x < transform.position.x)
    {
    transform.eulerAngles = new Vector2(0,180);
    transform.Translate(Vector2.right * speed * Time.deltaTime);
    }
    }
    }
     
    Last edited: Feb 25, 2015
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    First of all, use code tags when posting code!

    This should make your player move towards the last clicked point. You might need to calibrate the actual movement code, but the basics should be sound.

    Code (CSharp):
    1. public class clickToMove : MonoBehaviour
    2. {
    3.  
    4.     public float speed = 4f;
    5.  
    6.     Vector2 mp;
    7.  
    8.     // Use this for initialization
    9.     void Start ()
    10.     {  
    11.         mp = transform.position;
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update ()
    16.     {
    17.         Movement ();
    18.     }
    19.  
    20.     void Movement()
    21.     {
    22.         if (Input.GetMouseButton(1))
    23.         {
    24.             mp = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    25.         }      
    26.  
    27.         if (mp.x > transform.position.x + .5)
    28.         {
    29.             transform.eulerAngles = new Vector2(0,0);
    30.             transform.Translate(Vector2.right * speed * Time.deltaTime);
    31.         }
    32.  
    33.         if (mp.x < transform.position.x)
    34.         {
    35.             transform.eulerAngles = new Vector2(0,180);
    36.             transform.Translate(Vector2.right * speed * Time.deltaTime);
    37.         }
    38.     }
    39. }
     
    jaekx likes this.
  3. jaekx

    jaekx

    Joined:
    Dec 15, 2014
    Posts:
    27
    Awesome! I will try this when I get home it looks like it makes more sense to me thank you!!!!!
     
  4. jaekx

    jaekx

    Joined:
    Dec 15, 2014
    Posts:
    27
    This doesnt work it throws up errors about mp being used before its declared and talks about a child block?
     
  5. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    It would seem I didn't paste the first two lines of the script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    Add that to the start (probably since you didn't include them in your example :).

    The movement is the same as you had before, I think it works a little iffy so you probably want to rewrite that part.