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 /
  • Help Room /
avatar image
2
Question by Internetman · May 30, 2016 at 06:51 PM · c#androidinputtouchfinger

Player follow touch when holding down your finger on the screen (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 the player will follow the position on where you touch, so when you move your finger over the screen the object follows. But I have a problem!

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 jump between position when I press one point of the screen and then lift the finger of the screen to press another position of the mobile screen?? Any code examples?

I would also like it so that when the player has removed their finger of the screen they must touch the characters collider to make it moving again. Note that I'm currently using the "finger input" as a mouse position, so if the game was playable on a PC it would follow a mouseposition, is there any better way to do this? Should I use a raycast?

Here's the code for the player following the finger:

 using UnityEngine;
   using System.Collections;
   
   public class MoveScript : MonoBehaviour {
   
       public GameObject character;
       public float speed = 500.0f;
     
        void Start()
       {
           Input.multiTouchEnabled = false;
       }
   
   
       void Update () 
       {
           if(Input.touchCount == 1 &&  Input.GetTouch(0).phase == TouchPhase.Moved)
           {
   
               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);
   
           }
       }
   }
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 Internetman · Jun 12, 2016 at 06:34 PM 0
Share

Does anybody know how to help me with this?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by TheMazinTar · Jun 14, 2016 at 05:16 AM

What jumps out at me is that your speed is waaay too big. I think that the reason you're seeing instant travel is because it really is just that fast. Slow it down to, like, 1 or 2 to check if you can actually see it moving. I would think that should fix your issue.

As for what you would like to do (if I'm reading correctly that you only want the player to follow your finger if you first press on him and then move around), I would look at the position of the finger upon TouchPhase.Began and compare it to the location of the player (maybe with a simple Vector2.Distance). If it's within a certain distance (again, not too much! Start with a small number and work your way up), then set a bool as true, or something like that, to allow the Update method to call the movement code. Am I making sense?

Hope that helps!

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

170 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

Related Questions

Multitouch doesn't work 1 Answer

C# Limit touch movement on a raycast 2 Answers

Android Controls 1 Answer

How to pick up an object when touching a button(Mobile) 0 Answers

Non-responsive touch controls after showing ads. 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