Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 HemK · Sep 07, 2020 at 10:03 AM · scripting probleminputbeginnerjoysticks

How do I instantiate/fire when Joystick input ends?

Hello World!
I have a twin stick shooter with the right joystick used to aim and fire. I want the character to fire when the JS is released (in the direction the character was last looking at). Currently, I'm not able to shoot. I'm not sure why.

  public void playerLook()
     {
         bool canShoot = false;
         Vector3 LookDir = new Vector3(shootJS.Horizontal * lookSpeed, 0f, shootJS.Vertical * lookSpeed);
         Vector3 lastLook = Vector3.zero;
         if (LookDir != Vector3.zero)
         {
             nav.transform.forward = LookDir;
             lastLook = LookDir;
         }
         else { nav.transform.forward = lastLook;}
             
         float x = shootJS.Horizontal; float z = shootJS.Vertical;
         if (x != 0 || z != 0)  //The player is aiming now and should shoot when the JS is released.
         { canShoot = true; }
         if (canShoot==true)
         {
             if (x == 0 && z == 0) //if aiming, and the JS is released, check the condition for shooting and shoot.
             {
                 if (currentMag > 0)
                 {
                     Instantiate(bullet, shotSpawn.position, shotSpawn.rotation);
                     currentMag--;
                     return;
                 }
                 else if (currentMag == 0)
                 {
                     StartCoroutine(reload());
                 }
              
             }
 
 
         }
     }
             
 
 
 
 
 IEnumerator reload()
 {
 
     ammo = ammo - mag;
     currentMag = mag;
     yield return new WaitForSeconds(reloadTime);
 
 } 

Comment
Add comment · Show 2
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
avatar image ShadyProductions · Sep 07, 2020 at 10:34 AM 0
Share
      if (x != 0 || z != 0)
      { canShoot = true; }
      if (canShoot==true)
          if (x == 0 && z == 0)

It can't just magically become zero..

avatar image HemK ShadyProductions · Sep 07, 2020 at 04:35 PM 0
Share

Doesn't this check for the JS inputs beco$$anonymous$$g 0 while the bool is true?
If this doesn't work logically, what kind of statement should I use?
I want to enable a bool when the JS is moved, disable it when input stops and trigger a single event. This event shouldn't trigger till the next time this cycle is repeated. Do you have any suggestions?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Captain_Pineapple · Sep 07, 2020 at 05:30 PM

Well in general @ShadyProductions pointed out the correct issue, however the solution is missing here. Only thing you are missing here in your code is that your variable canShoot is a local one. Make that one a member of your class and reset it once you have reached the inner scope of if(x == 0 && y== 0) This should then work.


Your current version does not work because you always set canShoot to false and you will never enter both if statements for the input check in the same Update call.

Comment
Add comment · Show 2 · 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
avatar image HemK · Sep 08, 2020 at 02:31 PM 0
Share

@Captain_Pineapple
I did as you mentioned. However, I'm still seeing shots instantiated when I'm getting Joystick input. If you have the time, please do tell me how to rectify this.
Thank you
$$anonymous$$y whole code is as follows:

     public bool canShoot;
 
 
 
     // Start is called before the first frame update
     void Start()
     {
         stateText.text = "";
         nav = player.GetComponent<Nav$$anonymous$$eshAgent>();
         animator = player.GetComponent<Animator>();
         moveJS = GameObject.Find("Floating JS_$$anonymous$$ove").GetComponent<FloatingJoystick>();
         shootJS = GameObject.Find("Floating JS_Shoot").GetComponent<FloatingJoystick>();
         current$$anonymous$$ag = mag;
         canShoot = false;
     }
 
     // Update is called once per frame
     void Update()
     {
         movePlayer();
         playerLook();
         float x = shootJS.Horizontal; float z = shootJS.Vertical;
         if (canShoot == true)
         {
             shoot();
         }
         if (x == 0 || z == 0)
         { canShoot = false; }
             
             
         ammoCount.text = current$$anonymous$$ag + "/" + ammo;
         Debug.Log(current$$anonymous$$ag);
     }
 
     public void movePlayer()
     {
 
         float x = moveJS.Horizontal;
         float y = moveJS.Vertical;
         nav.velocity = new Vector3(x * moveSpeed, 0, y * moveSpeed);
         if (nav.velocity.x != 0 || nav.velocity.z != 0)
         { animator.SetBool("isRunning", true); }
         else { animator.SetBool("isRunning", false); }
 
     }
 
 
 
     public void playerLook()
     {
 
         Vector3 LookDir = new Vector3(shootJS.Horizontal * lookSpeed, 0f, shootJS.Vertical * lookSpeed);
         Vector3 lastLook = Vector3.zero;
         if (LookDir != Vector3.zero)
 
         {
             nav.transform.forward = LookDir;
             lastLook = LookDir;
 
         }
         else { nav.transform.forward = lastLook; }
 
         float x = shootJS.Horizontal; float z = shootJS.Vertical;
         if (x != 0 || z != 0)  //The player is ai$$anonymous$$g now and should shoot when the JS is released.
         { canShoot = true; }
     }
            
 
     public void shoot()
         {
         if (current$$anonymous$$ag > 0)
         {
             Instantiate(bullet, shotSpawn.position, shotSpawn.rotation);
             current$$anonymous$$ag--;
             canShoot = false;
             return;
         }
         else if (current$$anonymous$$ag == 0 && ammo != 0)
         {
             StartCoroutine(reload());
         }
         else if (ammo == 0)
         { return; }
              
             }
 
 
       
 
 
 
 
 
 IEnumerator reload()
 {
 
     ammo = ammo - mag;
     current$$anonymous$$ag = mag;
     yield return new WaitForSeconds(reloadTime);
 
 }       
 }
  
         
     
 
avatar image Captain_Pineapple HemK · Sep 10, 2020 at 09:23 PM 0
Share

i suggest you take out a paper and draw a diagramm of all possible states you can be in. This should help you in most cases to find your issue.. You now save the state by using a member variable but changed the conditions for some reason. This here does not really make sense does it? if (x == 0 || z == 0) It does neither check if you are ai$$anonymous$$g nor check if you are not ai$$anonymous$$g since one axis can always be zero and make this be true.


What you basically need is to do the following in this order:

check if you aim (-> set canShoot to true in this case)

check if canShoot is True

if canShoot is True: {

check if ai$$anonymous$$g ends => if (x == 0 && z == 0)

if ai$$anonymous$$g ended call Shoot. Shoot should then reset the flag canShoot = false }


This way the player has to move the joystick to get into shooting mode, creates a bullet when ai$$anonymous$$g ends and then has to move the stick again to start this process anew.

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

274 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 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

groundCheck in small beginner project not working properly 0 Answers

2d Raycasting on rotating rectangle. 1 Answer

On raycast hit remove Rigidbody 1 Answer

Touch dual Joystick script working eratically 0 Answers

Event from simultaneously pressing button + axis? 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