Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by $$anonymous$$ · Oct 21, 2018 at 11:27 AM · inputmobilefunctioncrossplatformnot responding

Shooting works with PC Keyboard, but not Mobile Touch Controls?!

https://youtu.be/v7RI8rZHWNw

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)){}
     }
 
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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)){}
     }
     
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

148 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges