- Home /
switch keyboard controls to simple tap controls,Android touch controls for simple mobile game testing
I have installed all the things I need for android build to work but I still need to do the code. I have followed a tutorial for the character controls, this can be seen here: https://www.youtube.com/watch?v=5M7vX_z6B9I Now I just need to convert my current character controls to mobile touch controls. I want to be able to tap right side of the screen to go up and left side to go down but I dont know how to do this? Can somebody show me how I would do this? :( My exact current player code sheet is this, just need it switched to touch:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI;
public class Player : MonoBehaviour {
private Vector2 targetPos;
public float Yincrement;
public float speed;
public float maxHeight;
public float minHeight;
public int health = 3;
public Text healthDisplay;
// Update is called once per frame
void Update () {
healthDisplay.text = health.ToString();
if (health <= 0)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
transform.position = Vector2.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.UpArrow) && transform.position.y < maxHeight) {
targetPos = new Vector2(transform.position.x, transform.position.y + Yincrement);
} else if (Input.GetKeyDown(KeyCode.DownArrow) && transform.position.y > minHeight){
targetPos = new Vector2(transform.position.x, transform.position.y - Yincrement);
}
}
}
Your answer
Follow this Question
Related Questions
Can't solve hitbox issues in mobile game 1 Answer
2 Objects spawning instead of one at each update 2 Answers
Possible to get touch area data? 1 Answer
How to move player using UI buttons, when using Input.mousePosition to shooting? (Android) 0 Answers
Trying to rotate my character to the position of my touch on mobile 1 Answer