• 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 yusolaifdfer · Jul 18, 2014 at 05:10 PM · inputvector3touchslerpgettouch

Movement along with touch of finger

I have script w$$anonymous$$ch I use to move my character along the Y axis with the touch of finger. The problem is I don't want a secondary effect that comes with it.

 if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved) {
             if((fp.y - myPos.y) < 1)
             {
             myPos = transform.position;
             Touch touch = Input.GetTouch(0);
             fp = Camera.main.ScreenToWorldPoint(new Vector2(touch.position.x, touch.position.y));
             myPos.y = fp.y;
             transform.position = Vector3.Slerp(transform.position,myPos,Time.deltaTime*myvelocity*sFac );
             }
             //transform.position = new Vector3(myPos.x,
 //                                             Mathf.SmoothStep(myPos.y, fp.y, Time.deltaTime*4),0);
         }

Alright so the problem is that when I touch anywhere on the screen it instantly transfers player at that Y point. W$$anonymous$$ch is kind of not what I want, I want user to at least click near the Y point of player (not on the player but anywhere on screen) and drag the finger up-down to move the player.

I tried putting up a condition before the Slerp if((fp.y - myPos.y)<2) { //slerp } else { //not$$anonymous$$ng }

but obviously it doesn't work, someone with more experience on Touch inputs might be able to clear t$$anonymous$$s up. Thanks guys.

Comment
Add comment · Show 7
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 yusolaifdfer · Jul 19, 2014 at 06:14 AM 0
Share

Mhh not getting enough views :T

avatar image yusolaifdfer · Jul 19, 2014 at 06:52 PM 0
Share

The touch and position updates at same time. I tried putting a Debug.Log(fp.y + lp.y) and they always returned same value even when I put it just below the nested if.

avatar image yusolaifdfer · Jul 24, 2014 at 03:47 AM 0
Share

If this couldn't be done, probably someone can make a new script for the same.. ?

avatar image calebheale · Jul 24, 2014 at 06:24 AM 0
Share

Could you be more specific about what you are looking for? It's hard for me to tell what you want because to me,

I want user to at least click near the Y point of player (not on the player but anywhere on screen)

sounds like you are saying you want to verify the click is near the player but can also be anywhere on the screen. Which seems to be contradictory.

avatar image yusolaifdfer · Jul 24, 2014 at 09:54 AM 0
Share

Mhh, by "anywhere on the screen" I mean the X point doesn't matter, suppose if X = 3, and Y is 9. The player moves to Y->9. And even if X was 8 the player would still have only moved to Y->9.

What I would want from it is that it don't snap long distances in Y points. Like jump from the bottom of the screen to top.

Show more comments

2 Replies

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

Answer by Andres-Fernandez · Jul 24, 2014 at 07:07 AM

The problem might be that you are setting a lerp position in the update function. You should be using a coroutine (or any other equivalent) instead. Whenever you detect the touch, get the position and start the coroutine that lerps your transform.position (you are doing that fine, just move it to a corountine). You just have to remember to update the target position and the time of the lerp wit$$anonymous$$n the coroutine.

Comment
Add comment · Show 12 · 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 yusolaifdfer · Jul 24, 2014 at 09:56 AM 0
Share

Yes, FixedUpdate. A Co-routine as such IEnumerator ? The I'll have to call the Coroutine in place of the transform.position. Well Okay I'll try that.

avatar image yusolaifdfer · Jul 24, 2014 at 10:44 AM 0
Share

Nope, doesn't helps. Almost identical to before except probably a bit smooth haha~

avatar image Andres-Fernandez · Jul 25, 2014 at 06:32 AM 0
Share

I don't know how your coroutine code is, but you should only call the coroutine when you detect the touchphase.began, and then check for the position of the finger within the loop. What usually works for me is (pseudocode):

 Inside Update:
 If (touchphase.began && fingerId == null) {
    call coroutine;
    storedFinger = fingerId;
    targetPosition = touch.position;
 }
 if (touchphase.ended && storedFinger == fingerId) { storedFinger = null;
 StopCoroutine();
 }
 
 Coroutine:
 timeToTargetPosition = distance / speed;
 time = 0;
 while (time < 1) {
    if (touch[storedFinger].position != targetPosition) {
      targetPosition = touch[storedFinger].position;
      timeToTargetPosition = distance / speed;
      time = 0;
    }
    time += Time.deltaTime / timeToTargetPosition;
    transform.position = Lerp();
    yield return null;
 }

The coroutine is a simple coroutine to make an object chase another object. You can figure out the code.

avatar image Andres-Fernandez · Jul 29, 2014 at 06:28 AM 1
Share

I use storedFinger to store the fingerId of the touch I use to make the object move. That way, I know which finger is moving the object. It is important to store the fingerId instead of the touch id because the id of the touch may vary depending on the number and order of the touches on the device (even if you didn't change the finger, its touch id may have changed between frames because of the rest of the fingers touching the device). That fingerId can also replace some other boolean to check if you are dragging a finger. Just use a value like -1 to represent a null value. So whenever the fingerId equals -1 it means you are not dragging the finger on screen, and if it equals 0 or bigger it means you are dragging a finger on screen.

TargetPosition is the value of the position you want your objective to move towards (if you are not moving it directly with the finger position, but with a stablished speed instead). You will use it in the lerp function, so it will look something like:

 transform.position = Vector3.Lerp(originPosition, targetPosition, time);

With time being a value between 0 and 1 obtained as I posted above.

The check inside the if is used to see if your target position has changed (you have moved the finger, therefore you want the object to go towards the new position of the finger). So if the position of the touch which contains your fingerId isn't the same as the your targetPosition (the finger has moved), you need to update the target position to the new position of the finger. You also need to change the time and the origin position, since you want to maintain the same speed all the time.

(Again, sorry for the pseudocode, I'll try to post some real code.)

avatar image yusolaifdfer · Jul 30, 2014 at 03:01 PM 1
Share

FINALLY. Got it working, well not "as is" but a little modification. Thanks!

Show more comments
avatar image
0

Answer by king_ · Jul 24, 2014 at 02:19 PM

use itween instead

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

24 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

Related Questions

unity 5.2 Input.GetTouch(0).position incorrect? 3 Answers

Move an object to Input.Touch location 0 Answers

Rotate player to LookAt a touch position 2 Answers

Odd touch input problem 0 Answers

Input.GetTouch(0) doesn't work 2 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