- Home /
help with touch
hi i am making a geometry dash kind of game for android and need to change the keyboard controls to touch any help? my script is
using UnityEngine; using System.Collections;
public class PlayerScript : MonoBehaviour {
public float jumpPower = 10.0f;
Rigidbody2D myRigidbody;
bool isGrounded = false;
float posX = 0.0f;
bool isGameOver = false;
ChallengeController myChallengeController;
GameController myGameController;
public AudioClip jump;
AudioSource myAudioPlayer;
public AudioClip scoreSFX;
public AudioClip deadSFX;
// Use this for initialization
void Start () {
myRigidbody = transform.GetComponent<Rigidbody2D>();
posX = transform.position.x;
myChallengeController = GameObject.FindObjectOfType<ChallengeController>();
myGameController = GameObject.FindObjectOfType<GameController>();
myAudioPlayer = GameObject.FindObjectOfType<AudioSource>();
}
void FixedUpdate () {
if (Input.GetKey(KeyCode.Space) && isGrounded && !isGameOver) {
myRigidbody.AddForce(Vector3.up * (jumpPower * myRigidbody.mass * myRigidbody.gravityScale * 20.0f));
myAudioPlayer.PlayOneShot(jump);
isGrounded = false;
}
//Hit in face check
if (transform.position.x < posX && !isGameOver) {
GameOver();
}
}
void GameOver() {
isGameOver = true;
myAudioPlayer.PlayOneShot(deadSFX);
myChallengeController.GameOver();
}
void OnCollisionStay2D(Collision2D other)
{
if (other.collider.tag == "Ground")
{
isGrounded = true;
}
}
void OnCollisionExit2D(Collision2D other)
{
if (other.collider.tag == "Ground")
{
isGrounded = false;
}
}
void OnTriggerEnter2D(Collider2D other) {
if (other.tag == "Star") {
myGameController.IncrementScore();
myAudioPlayer.PlayOneShot(scoreSFX);
Destroy(other.gameObject);
}
}
}
Answer by frederikedel · Dec 11, 2020 at 01:23 PM
Unsure what you mean by just changing it, you probably dont want to use the phones keyboard so you have to change it to completely different controls, like the zoom gesture or some on-screen buttons. There is a Brackey Tutorial, i linked it here, but you will have to do some additional work yourself, figuring out how you want to approach it.
Answer by Llama_w_2Ls · Dec 12, 2020 at 01:28 PM
You can use if (Input.GetMouseButtonDown(0))
as touch input instead of space. This acts as a mouse click essentially, but works for mobile too. @Jtdster4
Your answer
Follow this Question
Related Questions
Using Input.touchCount as a button 0 Answers
Differentiate swipe from tap android 0 Answers
Player doesn't move as intended using touch controls 0 Answers
Touch screen move - android 1 Answer
Touch Control for 2D Game 0 Answers