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 Internetman · Apr 04, 2016 at 07:36 PM · c#inputpositionmobiletouch

Mobile game touch problem (C#)

Hello my fellow Unities! I'm developing a 2D mobile game using touch. I'm trying to make it so that you can only hold down one finger on the screen when you do an object will follow the position on where you touch, so when you move your finger over the screen the object follows. But looks like I have hit a crossroad with two problems!

First, If I touch the screen on different location quickly the object will swap over to the position where i pressed, how can I make the ball just follow the finger when I hold down on the screen and not move between position when I press quickly?

Second, there is the problem when you hold down more than one fingers on the screen the ball will make "swaps" between positions. How do I set it to just work with one finger?

Here is the code:

 using UnityEngine;
   using System.Collections;
   
   public class MoveScript : MonoBehaviour {
   
       public GameObject character;
       public float speed = 50.0f;
   
   
       void Update () 
       {
           if(Input.touchCount == 1)
           {
   
               Vector3 target = Camera.main.ScreenToWorldPoint(new            Vector3(Input.mousePosition.x,Input.mousePosition.y, 10.0f));
               character.transform.Translate(Vector3.MoveTowards(character.transform.position, target, speed * Time.deltaTime) - character.transform.position);
   
           }
       }

Here is the code for checking input:

 using UnityEngine;
   using System.Collections;
   
   public class TouchScript : MonoBehaviour 
   {
       void OnGUI()
       {
           foreach(Touch touch in Input.touches)
           {
               string message = "";
               message += "ID: " + touch.fingerId + "\n";
               message += "Phase: " + touch.phase.ToString() + "\n";
               message += "TapCount: " + touch.tapCount + "\n";
               message += "Pos X: " + touch.position.x + "\n";
               message += "Pos Y: " + touch.position.y + "\n";
   
               int num = touch.fingerId;
               GUI.Label(new Rect(0 + 130 * num, 0, 120, 100), message);
   
           }
       }
   }

I'm grateful for any help I can get!

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
1

Answer by phil_me_up · Apr 04, 2016 at 11:37 PM

If you have no intention of allowing multi-touch, you can just disable it: http://docs.unity3d.com/ScriptReference/Input-multiTouchEnabled.html

Should you want multi-touch during some parts of the game, enable and then disable as needed.

Comment
Add comment · Show 4 · 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 Internetman · Apr 05, 2016 at 12:10 PM 0
Share

Ah nice, thank you, that seems to have fixed the the second problem. But the first problem still remains. How can I make the ball just follow the finger when I hold down on the screen and not jump between position when I press one point of the screen and then lift the finger of the screen to press another end of the mobile screen?

avatar image phil_me_up Internetman · Apr 05, 2016 at 12:42 PM 1
Share

I think you probably want to look at the TouchPhase: http://docs.unity3d.com/ScriptReference/Touch-phase.html

This way you can ignore initial taps on the screen.

Alternatively you can create your own system by comparing input positions to deter$$anonymous$$e if the ball should follow or not. You may find that you have to do this to some degree regardless of the ToughPhase, as tiny movements by your finger are inevitable and may trigger the 'movement' phase. The way to combat this is to look at your initial position and your new position, and if the magnitude of that vector exceeds a threshold, assume the ball needs to follow.

avatar image Internetman phil_me_up · Apr 05, 2016 at 01:25 PM 0
Share

I'll look into it! I'm thinking of making it so when I lift my finger of the screen while moving the player, I have to touch the player again to be able to move, is it possible for the code to be able to check if the TouchInput is on the "character" gameobject?

Show more comments
avatar image
0

Answer by mrMYSTERIOUSYT · Mar 09, 2017 at 08:09 AM

, @Internetman the problem is that i your code you are checking for touch count 1 which is wrong you should be checking for touch 0
just change the number to 0

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

133 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

Related Questions

Touch not working on guiTexture. 1 Answer

Tie touch logic into InputManager 0 Answers

How to differentiate touches on mobile devices 1 Answer

Input Touch.position 'inverted' relative to screen? 1 Answer

Mobile - button press while moving (First Person Controls) 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