Simple aPPS
Simple aPPS

Reputation: 33

Unity-How to move with touch input?

I have some problems with implementing touch movement to my code..Can somebody write to me what i need to do to get it working?

I want to move like Input.GetAxis("Horizontal");

Here is my working code to move with axis

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{

    public float speedY = 5f, speedX = 3f, boundX = 3f;
    
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        float input = Input.GetAxis("Horizontal");
        print(input);
       
        }

    }
    void Move()
    {
        Vector2 temp = transform.position;
        temp.y += speedY * Time.smoothDeltaTime;
        temp.x += speedX * Time.smoothDeltaTime * Input.GetAxis("Horizontal");
           
        transform.position = temp;
    }
}

And here is my solution what i thought it will work but it doesnt work...

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{

    public float speedY = 5f, speedX = 3f, boundX = 3f;
    
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        float input = Input.GetAxis("Horizontal");
        print(input);
        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);
            if (touch.position.x > (Screen.width / 2))
            {
                Move();
                Debug.Log("Go right");
            }
            if (touch.position.x < (Screen.width / 2))
            {
                Debug.Log("justleft");
            }
        }
}


    }
    void Move()
    {
        Vector2 temp = transform.position;
        temp.y += speedY * Time.smoothDeltaTime;
        temp.x += speedX * Time.smoothDeltaTime * Input.touchCount;
        transform.position = temp;
    }
}

I don't see debug when i click with code like last block of code.

Can somebody write me solution ? or help me with some tip.

thank you so much

Upvotes: 0

Views: 714

Answers (1)

Ali Kanat
Ali Kanat

Reputation: 1889

You can just implement this like this if i understood you correctly

void Update()
{
    //Because you always wanna move up
    transform.position += Vector2.up * Time.deltaTime;

    if (Input.touchCount > 0)
    {
        Touch touch = Input.GetTouch(0);
        if (touch.position.x > (Screen.width / 2))
        {
            //Since i do not know how much right you wanna go
            // This will just go left or right as long as there is a touch 
            transform.position += Vector2.right * Time.deltaTime * speedX;
            Debug.Log("Go right");
        }
        if (touch.position.x < (Screen.width / 2))
        {
            transform.position += Vector2.left* Time.deltaTime * speedX;
            Debug.Log("justleft");
        }
    }
}

Upvotes: 1

Related Questions