Reputation: 103
I've been testing my game on my computer, with the left & right arrows, but I want to switch it to a touch control.
I've looked into some tutorials, but some say I need to purchase an asset from the store or it doesn't really fit my needs.
I just need some guidance for the best direction I should take :)
This is my code for my controls right now, I was wondering if I can use 2 buttons since I only need to move left and right?
void Update()
{
transform.position = Vector2.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.RightArrow) && transform.position.x < maxWidth)
{
targetPos = new Vector2(transform.position.x + Xincrement, transform.position.y);
transform.position = targetPos;
}
else if (Input.GetKeyDown(KeyCode.LeftArrow) && transform.position.x > minWidth)
{
targetPos = new Vector2(transform.position.x - Xincrement, transform.position.y);
transform.position = targetPos;
}
}
Upvotes: 2
Views: 95
Reputation: 71
Use code that gets the position of the mouse when tapped, and check if its on the right or left part of the screen
Upvotes: 0
Reputation: 1060
Code for touching half right/left screen to move
void Update () {
transform.position = Vector2.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);
// Detect touch event
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
if (touch.position.x > (Screen.width / 2))
{
if (transform.position.x < maxWidth)
{
targetPos = new Vector2(transform.position.x + Xincrement, transform.position.y);
transform.position = targetPos;
}
}
else {
if (transform.position.x > minWidth)
{
targetPos = new Vector2(transform.position.x - Xincrement, transform.position.y);
transform.position = targetPos;
}
}
}
}
Upvotes: 1