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 aman_jha · Jun 29, 2014 at 05:13 PM · c#androidiphonetouch

Touch Hold for Power

Hey guys,

In my game, there is a cannon, and you use the accelerometer to tilt the angle of it. By touching the screen anywhere, and holding, I want to increment the power of the cannon so it shoots further and faster. Using the power float doesn't trouble me, it is holding the finger on the screen and calculating based on how long it is hold.

I know I have to use some sort of TouchPhase.Stationary, but I'm not sure how. I've gotten this code and I'm not sure where or how to calculate power from it:

 //Power Controller
         if (Input.touchCount == 1) {
             Touch touch = Input.GetTouch(0);
             if(touch.phase==TouchPhase.Began){
                 newTouch = true;
                 touchTime = Time.time;
                 //Debug.Log("touchTime=" + touchTime);
             }else if (touch.phase == TouchPhase.Stationary){
                 if(newTouch==true && Time.time-touchTime>1){
                     //Debug.Log("longpress detected");
                     newTouch = false;
                     longPressDetected = true;
                 }else if (newTouch == false && longPressDetected==false){ // began not detected
                     newTouch = true;
                     touchTime = Time.time;
                     //Debug.Log("touchTime=" + touchTime);
                 }
             }else{
                 newTouch = false;
                 longPressDetected = false;
                 //Debug.Log("setting newTouch false");
             }
         }





Honestly I'm not even sure if the code works because the phone will not respond...

Comment
Add comment · Show 1
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 AndyMartin458 · Jun 30, 2014 at 12:40 AM 0
Share

There could be an issue in how you are measuring touches. This code you have should be running in an object's update loop. Also, for good measure check for Input.touchCount >= 1 and print out a Debug.Log to show how many touches were measured. @Yoman

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Lylek · Jun 30, 2014 at 05:36 AM

Here is what I use for apps:

 function Update () {
     //Touch Screen***
     if(Input.touchCount > 0) {
         var tapCount = Input.touchCount;
         for(var i = 0; tapCount > i; i++) {            
             if(i < 1) {
                 var touch = Input.GetTouch(i);
             }
             if(GetComponent(GUITexture)) {
                 if(touch.phase == TouchPhase.Ended && guiTexture.HitTest(touch.position)) {
                     //Call Up Function
                     //Up();
                 }
                 else {
                     if(touch.phase == TouchPhase.Stationary && guiTexture.HitTest(touch.position)) {
                         //Call Hold Function
                         //Hold();
                     }
                     else {
                         if(touch.phase == TouchPhase.Began && guiTexture.HitTest(touch.position)) {
                             //Call Down Function
                             //Down();
                         }
                     }
                 }
             }
         }
     }
 }
 function Hold() {
     //Increase your Power
 }

This will work if pressing GUITextures. If your pressing a text, just change it to GUIText (obviously :)). I'm not sure it will work if your not pressing either though... like pressing into space, or on a gameObject. Maybe, (MeshRenderer)?

Also, you don't have to call a function, you can just write it within the if statement. I just feel it is cleaner otherwise.

Comment
Add comment · Show 11 · 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 aman_jha · Jul 01, 2014 at 06:29 PM 0
Share

Alright, that looks like it would work really well! I don't exactly know how I would test it atm because my cannon is being buggy itself! Once I get it fixed I'll let you know how it works. And about it touching anywhere, I can just instantiate a guitexture at the touch position so that's not a problem :)

avatar image aman_jha · Jul 01, 2014 at 06:33 PM 0
Share

Also would I need the Up(); and Down(); functions (obviously not with those names) but is it necessary for me to call that in my code?

avatar image Lylek · Jul 01, 2014 at 08:57 PM 0
Share

No. Not if you don't need them. Treat them like On$$anonymous$$ouseDown, and On$$anonymous$$ouseUp functions. If you need something to happen as soon as you press down, and/or when you lift off the button.

Otherwise, feel free to delete that part of the code. :)

avatar image Lylek · Jul 01, 2014 at 09:11 PM 1
Share

If you don't want to press a guiTexture or anything, like just anywhere on the screen, maybe just this will work?

 function Update () {
     //Touch Screen***
     if(Input.touchCount > 0) {
         var tapCount = Input.touchCount;
         for(var i = 0; tapCount > i; i++) {    
             if(i < 1) {
                 var touch = Input.GetTouch(i);
             }
             if(touch.phase == TouchPhase.Stationary) {
                 //Increase Power
                 //Hold();
             }
         }
     }
 }

Haven't tried it though.

avatar image aman_jha · Jul 02, 2014 at 01:30 PM 0
Share

I'll try that. I tried inputing your code into the code I had for my GUITexture before, but I got a load of errors.

 using UnityEngine;
 using System.Collections;
 
 public class Fire : $$anonymous$$onoBehaviour {
 
     public float $$anonymous$$Power = 100;
     public float powerValue;
 
     void Update () {
         //Touch screen applicable only:
         if(Input.touchCount > 0) {
             float tapCount = Input.touchCount;
             for (float i = 0; tapCount > 1; i++) {
                 if(i < 1){
                     float touch = Input.GetTouch(i);
                 }
                 if(GetComponent(GUITexture)) {
                     if(touch.phase == TouchPhase.Ended && guiTexture.HitTest(touch.position)) {
                         //Call Up Function
                         GameObject.Find("GameController").GetComponent<GameController>().Fire(powerValue);
                     }
                     else {
                         if(touch.phase == TouchPhase.Stationary && guiTexture.HitTest(touch.position)) {
                             //Call Hold Function
                             Hold ();
                         }
                         else {
                             if(touch.phase == TouchPhase.Began && guiTexture.HitTest(touch.position)) {
                                 //This is when the touch has began
                                 //Pretty much do nothing.. yet (later instantiate growing image at position)
                             }
                         }
                     }
                 }
             }
         }
     }
 
     
     void Hold() {
         //Increase your Power
         if (powerValue <= $$anonymous$$Power)
             powerValue = $$anonymous$$Power;
         else {
             powerValue++;
         }
     }
 }
 

And yes, I had converted your code from JS to C# because all of my other code is in C# and I like consistancy. There shouldn't be a problem though right?

Show more comments

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

23 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

Related Questions

Joystick trouble... 0 Answers

Question about Unity Touch Interface For Android 1 Answer

How can I make a thumbstick with this code? HELP 1 Answer

GUI.Button Touch Input Problem 1 Answer

How to make object face touch position 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