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 Matthew Dennis · Apr 28, 2011 at 02:19 PM · javascriptiphonetouchtouchphase

Holding a Touch down on the iphone

I have a strange problem where i want to be able to touch the iphone to make an object move by a force but i want to be able to keep me finger down to make it move constantly until i release my finger.

I have played about with the touchphases where i first tried to keep it as touch began, then tried to say that if not equal to ended or/and canceled but both did not work.

Anyone know how to keep a press held on?

Here is the state of my code that it is at now;

for (var i = 0; i < Input.touchCount; ++i) { var touch: Touch = Input.GetTouch(0); if (touch.phase == TouchPhase.Began/*&& jumpo.collider.gameObject.tag == "player"*/) { if(touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled) { jumpo.rigidbody.AddForce(Vector3.up * 1000); on.emit = true; } }

else if (touch.phase == TouchPhase.Ended) { on.emit = false; jumpo.rigidbody.AddForce(Vector3.up * 400); }

}

the above code is in a function FixedUpdate so the force can actually be a force instead of doing nothing.

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 rejj · Apr 28, 2011 at 02:24 PM 0
Share

should it be var touch : Touch = Input.GetTouch(i); ins$$anonymous$$d? you're grabbing touch 0 every time around the loop...

avatar image Matthew Dennis · Apr 28, 2011 at 02:34 PM 0
Share

i did that change and it still does the same as it already is :(

2 Replies

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

Answer by rejj · Apr 28, 2011 at 02:33 PM

It looks like you're wrapping everything in a check first for TouchPhase.Began, so your force code will only run when the touch is in the Began phase.

/* this will apply the force whenever a touch starts or is held, and remove it whenever any touch ends (not a concern if you don't use multitouch) */
for (var i = 0; i < Input.touchCount; ++i) {
    var touch : Touch = Input.GetTouch(i);
    if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Cancelled) {
        /* apply force here */
    }
    else if (touch.phase == TouchPhase.Ended) {
       /* remove force here */
    }
}

perhaps? (untested, sorry)

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 Matthew Dennis · Apr 28, 2011 at 02:41 PM 0
Share

you sir, deserve a pint!

avatar image equalsequals · Apr 28, 2011 at 02:41 PM 0
Share

This is part way correct, you got why he isn't getting the result. I see a problem though. Your code will enable the user to add massive amounts of force to the character by using multiple touches since you're iterating through all of the touches in the array.

avatar image
2

Answer by equalsequals · Apr 28, 2011 at 02:39 PM

There are a few problems I see here:

First, you are checking if touch.phase isn't Ended or Canceled inside of the conditional statement which checks for Began. This is a logical error because in order to get to your condition about Ended/Canceled, touch.phase would have to be Began and thus can only ever be true when touch is Began.

Since you want the force added whenever the player is touching the screen, this would be the way to do it:

if(Input.touchCount >= 1) { var touch : Touch = Input.touches[0];

 if(touch.phase != TouchPhase.Ended &amp;&amp; touch.phase != TouchPhase.Canceled)
 {
     jumpo.rigidbody.AddForce(Vector3.up * 1000);
     on.emit = true;
 }
 else
 {
     on.emit = false;
     jumpo.rigidbody.AddForce(Vector3.up * 400);
 }

}

Additionally, since you are only ever using the one touch, no need to recursively move through the touches, you can just check if there is 1 or more touches in the array, and grab the first one like you were doing. It's a bit more efficient.

Cheers.

==

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 Matthew Dennis · Apr 28, 2011 at 02:53 PM 0
Share

This works perfectly aswel thank you :)

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

Iphone Unity and Best book . 1 Answer

IsPointerOverGameObject doesn't work on TouchPhase.Ended? 1 Answer

Converting Simple Web game to Iphone 2 Answers

iPhone touch raycasting 1 Answer

How to get my character to move left to right? 0 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