- Home /
Question by
calum1904 · Jun 25, 2017 at 10:23 AM ·
movementshootingtouch controlsmultitouchfiring
Can't move and fire at the same time!
So I am making a 2d infinite runner which requires moving up and down by pressing the left side of the screen and shooting by pressing the right. I have the player moving up and down by tapping the corresponding half (the top or bottom of the left side) or dragging (haven't started this). But when I tap the right the ship stops. This doesn't happen in PC mode but does on mobile, I've been stuck on this for a while so if anyone can help I'd greatly appreciate it.
//Touch Controller Script
public class touchControls : MonoBehaviour {
public playerManager player;
float middleScreen = Screen.height / 2;
float thirdScreen = Screen.width / 3;
Touch myTouch;
Touch mySecondTouch;
void Start(){
Input.simulateMouseWithTouches = true;
Debug.Log ("middle " +middleScreen);
}
void Update(){
if (Input.touchCount > 0) {
if (Input.GetTouch (0).phase == TouchPhase.Began) {
myTouch = Input.GetTouch (0);
if (myTouch.position.y > middleScreen) {
if (myTouch.position.x < thirdScreen) {
moveUp ();
}
} else if (myTouch.position.y < middleScreen) {
if (myTouch.position.x < thirdScreen) {
moveDown ();
}
}
} else if (Input.GetTouch (0).phase == TouchPhase.Ended) {
noPress ();
} else if (Input.GetTouch (0).phase == TouchPhase.Canceled) {
noPress ();
}
}
}
Player Manager
//shoot linked to event handler (invisible button)
public class playerManager : MonoBehaviour {
public float speed = 15;
public GameObject bullet;
public Transform firePoint;
public bool canFire = true;
public static float fireRate = 0.3f;
public void shoot (){
if (canFire == true) {
StartCoroutine (shootNow ());
}
}
public IEnumerator shootNow(){
canFire = false;
//Debug.Log ("firing");
Instantiate(bullet, firePoint.position, firePoint.rotation);
yield return new WaitForSeconds(fireRate);
canFire = true;
}
}
Also if you could point my in the right direction to dragging touch controls that would be awesome :D
Comment
Your answer
Follow this Question
Related Questions
How could I implement diagonal movement in this code? 1 Answer
Touch buttons for step movememt 3 Answers
Multi-touch for camera not working? 0 Answers