Search Unity

Need Help for unity 2D touch input

Discussion in 'Scripting' started by kasana_94, Jul 2, 2015.

  1. kasana_94

    kasana_94

    Joined:
    Apr 13, 2014
    Posts:
    24
    Hello friends
    I am stuck with this script in which i want to move my player from left to right (ONLY HORIZONTALLY) by dragging it on screen.

    By far i am able to move it by keyboard input on my desktop.

    I am somewhat lost on how to achieve it.

    Thanks in advance


    using UnityEngine;
    using System.Collections;

    public class movement : MonoBehaviour {

    Vector2 position;
    public float speed;
    void Start () {

    position = transform.position;

    }


    void Update () {

    float moveX = Input.GetAxis ("Horizontal");
    position.x += moveX * Time.deltaTime * speed;
    position.x = Mathf.Clamp (position.x, -7.5f, +7.5f);
    transform.position = position;

    }


    }
     

    Attached Files:

  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
  3. kasana_94

    kasana_94

    Joined:
    Apr 13, 2014
    Posts:
    24
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class movement : MonoBehaviour {
    5.  
    6. Vector2 position;
    7. public float speed;
    8. void Start () {
    9.  
    10. position = transform.position;
    11.  
    12. }
    13.  
    14.  
    15. void Update () {
    16.  
    17. float moveX = Input.GetAxis ("Horizontal");
    18. position.x += moveX * Time.deltaTime * speed;
    19. position.x = Mathf.Clamp (position.x, -7.5f, +7.5f);
    20. transform.position = position;
    21.  
    22. }
    23.  
    24.  
    25. }
     
  4. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Thanks for the code tags ;) So, you want to drag the player on the screen on the x-axis by using your mouse instead of your keyboard, right? Did you try out Input.GetAxis("Mouse X"); ?
     
  5. kasana_94

    kasana_94

    Joined:
    Apr 13, 2014
    Posts:
    24
    nope.. i want to drag the player using touch controls (only on x axis)
    i am making it for android/ios
     
    Last edited: Jul 2, 2015
  6. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    So your player is for example in the center of the screen. Now you wanna drag him from center to right for example and he will move to the right with some latency? You should check out some touch tutorials and maybe explain, how you want to move your character exactly. Like, touch on a point and move there, or drag to the right and move to the right depending on touch begin and end?
     
  7. kasana_94

    kasana_94

    Joined:
    Apr 13, 2014
    Posts:
    24
    yes, basically i am making a brick breaker game , in which i want to control the paddle with touch-drag movement from left to right.
     
  8. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
  9. kasana_94

    kasana_94

    Joined:
    Apr 13, 2014
    Posts:
    24
    i did see that. Issue with me is that i am not from a programming background. Art is more of my thing, thats why i am having difficulty in this.
     
  10. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Try this. Not tested but could keep you going:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class movement : MonoBehaviour {
    5.  
    6.     Vector2 charPosition;
    7.     public float speed;
    8.     void Start () {
    9.         charPosition = transform.position;
    10.      
    11.     }
    12.  
    13.     void Update () {
    14.         float moveX ;
    15.         for (var i = 0; i < Input.touchCount; ++i) {
    16.             Touch touch = Input.GetTouch(i);
    17.             if (touch.phase == TouchPhase.Began) {
    18.                 moveX = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 0)) ;
    19.                 charPosition.x += moveX * Time.deltaTime * speed;
    20.                 charPosition.x = Mathf.Clamp (charPosition.x, -7.5f, +7.5f);
    21.                 transform.position = charPosition;
    22.             }
    23.         }    
    24.     }
    25. }
     
  11. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    I just changed the position var to charPosition, as you should never use API specific keywords like this for variables :)
     
  12. kasana_94

    kasana_94

    Joined:
    Apr 13, 2014
    Posts:
    24
    thanks for help
    i will try this out