Search Unity

Make a Character Move on a 1D plane Based on Where You Click (Relative to character's position)

Discussion in '2D' started by QuindecimX, Jun 10, 2018.

  1. QuindecimX

    QuindecimX

    Joined:
    Jul 12, 2017
    Posts:
    22
    As the title says, I'm making a 2D, point-and-click game. I've coded it so that the character moves left when you click on the left side of the screen, and moves right when you click on the right side of the screen. The issue is that I want the character move depending on where you click relative to their position on-screen. Look at the GIF I made below to get a better idea of what I'm describing. The white box with the green outline represents the game screen/scene.

    Untitled-1.gif
    From this you can see the problem. For example, if the character is near the right side of the screen and you quickly need him to go left, you'd have to make sure you click only on the left-half side of the screen, or else the character won't go left. That is what I DON'T want.

    Here is my code below:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Movement : MonoBehaviour
    6. {
    7.     private float speed = 1.0f;
    8.     public Animator anim;
    9.  
    10.     void Start()
    11.     {
    12.         anim = GetComponent<Animator>();
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.  
    18.         if (Input.GetKey(KeyCode.Mouse0))
    19.         {
    20.             if (Input.mousePosition.x > Screen.width / 2)
    21.             {
    22.                 anim.SetBool("walk_left", false);
    23.                 Debug.Log("moving right");
    24.                 transform.Translate(Vector3.right * speed * Time.deltaTime);
    25.             }
    26.             else
    27.             {
    28.                 anim.SetBool("walk_left",true);
    29.                 Debug.Log("moving left");
    30.                 transform.Translate(-Vector3.right * speed * Time.deltaTime);
    31.             }
    32.         }
    33.     }
    34. }
    35.  
     
  2. bakir-omarov

    bakir-omarov

    Joined:
    Aug 1, 2015
    Posts:
    48
    You can make it even simple, just check if player pressed on the left side of GameObject, or on the right side. Check the code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Movement : MonoBehaviour
    6. {
    7.     private float speed = 1.0f;
    8.     public Animator anim;
    9.  
    10.     void Start()
    11.     {
    12.         anim = GetComponent<Animator>();
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         // getting mouse coordinates in World coordinate system
    18.         Vector2 mouseVector2 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    19.         float mouseX = mouseVector2.x;
    20.  
    21.         if (Input.GetKey(KeyCode.Mouse0))
    22.         {
    23.             // if you are pressing on the left side of PLAYER then go left, if on the right, then go right
    24.             if (mouseX > transform.position.x)
    25.             {
    26.                 anim.SetBool("walk_left", false);
    27.                 Debug.Log("moving right");
    28.                 transform.Translate(Vector3.right * speed * Time.deltaTime);
    29.             }
    30.             else
    31.             {
    32.                 anim.SetBool("walk_left", true);
    33.                 Debug.Log("moving left");
    34.                 transform.Translate(-Vector3.right * speed * Time.deltaTime);
    35.             }
    36.         }
    37.     }
    38. }
    39.  
     
    QuindecimX likes this.
  3. QuindecimX

    QuindecimX

    Joined:
    Jul 12, 2017
    Posts:
    22
    Yes! Everything worked out. Thanks for your help.