Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Valdemars Magone · Mar 01, 2011 at 08:39 PM · buttoniphonetouchmessagesending

Continuous shooting by holding iPhone button

I'm trying to create iPhone button that would continuously Instantiate bullets when finger touches this button and is held down, without conflicting with other buttons, for example, rotation joystick. At the moment I have this code that sends only one Message each time when button gets touched ( I also have script from 3rd Person Camera Prefab, which sends message when finger is RELEASED, but that one is much longer and more complicated). Maybe somebody has an idea how to keep sending message to target for few times per second untill finger is released from button...

Thanks in advance!

var recoveryTime = 10;

private var delay = 0; var target : MonoBehaviour; //Target of the message to be sent var message : String ; //Message to be sent

function Update () { if (delay>0){delay -=1;}

 if (delay==0){
     if(iPhoneInput.touchCount == 1){
         var currentTouch:iPhoneTouch = iPhoneInput.touches[0]; 

         if(currentTouch.phase == iPhoneTouchPhase.Began && guiTexture.HitTest(currentTouch.position)){

             target.Invoke(message, 0);


             delay = recoveryTime;
         }
     }
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Bampf · Mar 01, 2011 at 10:01 PM

That code is only testing for TouchPhase.Began. Try allowing the phases Stationary and Moved as well. Or just check for the touch ending:

if ((currentTouch.phase != iPhoneTouchPhase.Ended 
     && currentTouch.phase != iPhoneTouchPhase.Cancelled)
  && guiTexture.HitTest(currentTouch.position)
{
    target.Invoke(message, 0);
    delay = recoveryTime;
}

Arguably you don't need to test the touch phase at all though. You might be able to fire if there's a touch in ANY stage as long as it's on the texture:

if (guiTexture.HitTest(currentTouch.position)
{
    target.Invoke(message, 0);
    delay = recoveryTime;
}

Phases are mostly used when detecting drags, zooms, or when you need fancy logic like, "cancel selection and start scrolling when they move their finger more than N pixels".

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 Valdemars Magone · Mar 02, 2011 at 09:10 AM 0
Share

This works good, by changing recovery time I can change fire rate, but there is one problem : when I shoot and want to change direction with rotation joystick, shooting stops when other finger has touched screen in different location outside this guiTexture...Is there some way to fix this?

avatar image Bampf · Mar 02, 2011 at 04:10 PM 0
Share

Yes, fixable. The posted code is using only the first touch in the array: iPhoneInput.touches[0]. Ins$$anonymous$$d you would need to look at all touches in the array, and find the one with the same fingerid as the one that was touching the guitexture last time.

avatar image
0

Answer by user-10474 (yahoo) · Mar 19, 2011 at 01:11 AM

Hi, Can you please give me an example on how you managed to implement the joystick and a fire button on this? I am working on a 2d shooter also but can't figure it out the movement (joystick) code :( Thanks

Comment
Add comment · Show 1 · 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 Valdemars Magone · Mar 19, 2011 at 09:35 AM 0
Share

Well, I have a 3rd person camera controller prefab from tornadotwins and with this button I send message to that player script. If you want to know specific details, email me at vmgn021@gmail.com

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

No one has followed this question yet.

Related Questions

How can I swipe function that lets the player select buttons? 0 Answers

Wrong Button touch? 1 Answer

iPhone Touch Screen? 2 Answers

Touch GUI texture to toogle animation 0 Answers

help with touch button (android) 1 Answer


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