- Home /
How do I change this 2D script to touch controls to go up and down with swipe or on screen buttons?
Plz help me with this player script i'm a beginner on unity and i'm making a 2D game i want to change the controls from key-inputs to touch control but i don't know how to make it work, i want to make the player move up or down a specific amount plz help and thank you for reading.
This is the script that i use for the movement
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class Player: MonoBehaviour {
private Vector2 tragetPos;
public float Yincrement;
public float speed;
public float maxHeight;
public float minHeight;
public int health = 3;
public Text healthDisplay;
private void Update()
{
healthDisplay.text = health.ToString();
if (health <= 0)
{
SceneManager.LoadScene("GameOver");
}
transform.position = Vector2.MoveTowards(transform.position, tragetPos, speed * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.UpArrow) && transform.position.y < maxHeight) {
tragetPos = new Vector2(transform.position.x, transform.position.y + Yincrement);
} else if (Input.GetKeyDown(KeyCode.DownArrow) && transform.position.y > minHeight) {
tragetPos = new Vector2(transform.position.x, transform.position.y - Yincrement);
}
}
}
Answer by hectorux · Nov 11, 2018 at 11:58 PM
If you want just one finger, or one touch, the mouse inputs will work, something like mouse.delta.x(i dontt remember exaclty this line)
Can you plzzzz let me the code that i need to put in the script if you remember it i still haven't figured it out, what do i put ins$$anonymous$$d of the Get$$anonymous$$eyDown if statement i did put Get$$anonymous$$ouseDown but it doesn't work for me all i need is swipe up and down a specific amount,plz help me AND THAN$$anonymous$$ YOU SOOO $$anonymous$$UCH FOR CO$$anonymous$$$$anonymous$$ENTING ON THE QUESTION.
Your answer
Follow this Question
Related Questions
Touch input broken with multi-touch 1 Answer
Possible to get touch area data? 1 Answer
Android Touch Screen Button for touch and hold 1 Answer
Can't solve hitbox issues in mobile game 1 Answer
Touchscreen controls in a 2D game 0 Answers