- Home /
Shooting works with PC Keyboard, but not Mobile Touch Controls?!
So, I've implemented Mobile Controls, but something's wrong, and I can't reproduce it.
Why does shooting works perfectly with PC Keyboard, but not at all with Mobile Controls? The Touchscreen Shooting Button even Registers Input, and triggers literally the same Parts of the Code as the PC Keyboard Shooting Button!
This is the shooting Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class PlayerShoot : MonoBehaviour {
public PlayerMovementNew _playerMovement; //Needed in order to access the Direction the Player Faces(Left or Right)
//public Animator animator;
public GameObject WeaponDefaultLeft; //Default Weapon, Facing Left
public GameObject WeaponDefaultRight; //Default Weapon, Facing Right
public Transform WeaponSpawn;
public float WaitTime; //The Time you need to Wait before Shooting again.
public float WaitedTime; //The Time you have waited Since the last Shot.
//Update is called once per frame
void Update () {
WaitedTime += Time.deltaTime; //Always increase the WaitedTime.
if ( CrossPlatformInputManager.GetButton ("Fire1") || Input.GetButton ("Fire1") )
{
if (WaitedTime > WaitTime) { //If you waited longer than the WaitTime...
//...and you Face left:
if (_playerMovement.playerDirection == -1)
{
//animator.Play ("Shoot", 0); //Set Animation to Shoot
GameObject NewWeapon = Instantiate (WeaponDefaultLeft, WeaponSpawn); //Shoot Left! ...
NewWeapon.transform.parent = null; //...and make the Projectile independent
} //...and you Face right:
else if (_playerMovement.playerDirection == 1) {
//animator.Play ("Shoot", 0); //Set Animation to Shoot
GameObject NewWeapon = Instantiate (WeaponDefaultRight, WeaponSpawn); //Shoot Right! ...
NewWeapon.transform.parent = null; //...and make the Projectile independent
}
WaitedTime = 0; //Reset WaitedTime
//animator.Play ("Shoot", 0); //Set Animation to Idle
}
}
}
//Debug GUI. This Part has nothing to do with the Shooting itself, but just Informations about the Shooting, that shouldn't be visible unless for debugging:
void OnGUI()
{
GUI.Box(new Rect(10,10,200,120), "Shooting Info");
if ( CrossPlatformInputManager.GetButton ("Fire1") || Input.GetButton ("Fire1") )
{
if(GUI.Button(new Rect(20,40,180,20), "Shooting: Yes")){}
}else
{
if(GUI.Button(new Rect(20,40,180,20), "Shooting: No")){}
}
if(GUI.Button(new Rect(20,70,180,20), "WaitTime: " + WaitTime)){}
if(GUI.Button(new Rect(20,100,180,20), "WaitedTime: " + WaitedTime)){}
}
}
Answer by $$anonymous$$ · Oct 26, 2018 at 02:58 PM
I solved the Problem myself. It took me long to figure out, but the Solution was to, instead of directly calling the Shooting Code when the if-Condition Matches, simply moving the Shooting Code in a Method and then calling that Method with the if-Statement that used to call the Shooting Code directly.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class PlayerShoot : MonoBehaviour {
public PlayerMovementNew _playerMovement; //Needed in order to access the Direction the Player Faces(Left or Right)
//public Animator animator;
public GameObject WeaponDefaultLeft; //Default Weapon, Facing Left
public GameObject WeaponDefaultRight; //Default Weapon, Facing Right
public Transform WeaponSpawn;
public float WaitTime; //The Time you need to Wait before Shooting again.
public float WaitedTime; //The Time you have waited Since the last Shot.
//Update is called once per frame
void Update () {
WaitedTime += Time.deltaTime; //Always increase the WaitedTime.
if ( CrossPlatformInputManager.GetButton ("Fire1") || Input.GetButton ("Fire1") )
{
PlayerShooting();
}
}
void PlayerShooting()
{
if (WaitedTime > WaitTime) { //If you waited longer than the WaitTime...
//...and you Face left:
if (_playerMovement.playerDirection == -1)
{
//animator.Play ("Shoot", 0); //Set Animation to Shoot
GameObject NewWeapon = Instantiate (WeaponDefaultLeft, WeaponSpawn); //Shoot Left! ...
NewWeapon.transform.parent = null; //...and make the Projectile independent
} //...and you Face right:
else if (_playerMovement.playerDirection == 1) {
//animator.Play ("Shoot", 0); //Set Animation to Shoot
GameObject NewWeapon = Instantiate (WeaponDefaultRight, WeaponSpawn); //Shoot Right! ...
NewWeapon.transform.parent = null; //...and make the Projectile independent
}
WaitedTime = 0; //Reset WaitedTime
//animator.Play ("Shoot", 0); //Set Animation to Idle
}
}
//Debug GUI. This Part has nothing to do with the Shooting itself, but just Informations about the Shooting, that shouldn't be visible unless for debugging:
void OnGUI()
{
GUI.Box(new Rect(10,10,200,120), "Shooting Info");
if ( CrossPlatformInputManager.GetButton ("Fire1") || Input.GetButton ("Fire1") )
{
if(GUI.Button(new Rect(20,40,180,20), "Shooting: Yes")){}
}else
{
if(GUI.Button(new Rect(20,40,180,20), "Shooting: No")){}
}
if(GUI.Button(new Rect(20,70,180,20), "WaitTime: " + WaitTime)){}
if(GUI.Button(new Rect(20,100,180,20), "WaitedTime: " + WaitedTime)){}
}
}
Your answer
Follow this Question
Related Questions
Issue: CrossPlatformInput...Multiple Inputs not taken, 0 Answers
Raycast from touch, return if hit a specific object. 0 Answers
does getmousebuttondown counts as physicaly clicking (for mobile games)? 1 Answer
Detect if running on mobile vs desktop 2 Answers
How do I "trickle" a function's effect over n seconds? 2 Answers