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 ctink · Jun 17, 2015 at 04:57 AM · buttonmobile

Moving character with touch controls.

I am currently working on a 2D platformer and I am having issues with moving my character. I've exported this to Android as well. What I had originally were two separate buttons for movement back and forth using event triggers and that worked just fine. Well I was told to change that into one button so that the character can move back and forth without lifting your finger. Users should be able to move the character simply my moving their finger back and forth on the button. Any help on this issue is greatly appreciated. Thank you!

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 saravanan-P · Jun 17, 2015 at 09:16 AM 0
Share

use touch phase.. http://docs.unity3d.com/ScriptReference/TouchPhase.html

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Chris333 · Jun 17, 2015 at 07:47 AM

Hi,

i have a mobile game where you can move the player by moving the finger over the screen not depending if you are on a button. The following snippet should do that.

 private Vector3 velocity = Vector3.zero;
 public float smoothTime = 0.3F;
 
 void FixedUpdate ()
 {
     Vector2 touchDeltaPosition = Vector2.zero;
 
     //touch movement
     if (Input.touchCount > 0)
     {
         // Get movement of the finger since last frame
         touchDeltaPosition = Input.GetTouch(0).deltaPosition * speed;
     }
     Vector3 actualPosition = GetComponent<Rigidbody2D>().position;
     Vector3 target = new Vector3(actualPosition.x + touchDeltaPosition.x, actualPosition.y + touchDeltaPosition.y, 0.0f);
     GetComponent<Rigidbody2D>().position = Vector3.SmoothDamp(actualPosition, target, ref velocity, smoothTime);
 }


To make it work with your UI button you could use the following method. It simply checks if the pointer with the given id is over a gameObject.

http://docs.unity3d.com/ScriptReference/EventSystems.EventSystem.IsPointerOverGameObject.html

I didnt tested this but it should work.

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 ctink · Jun 23, 2015 at 04:30 PM 0
Share

How would I go about incorporating IsPointerOverGameObject into the script. I've been messing around with it but can't get it to work properly.

avatar image jprocha101 · Jun 23, 2015 at 10:57 PM 0
Share

The documentation Chris linked you to has a great example of how to use IsPointerOverGameObject. If you followed the documentation and still can't get it working please post some code from your script so we see what you are doing?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

UI works in editor, but not on mobile device 1 Answer

[C#] Both button check with delay. 1 Answer

problem with button touch zone 0 Answers

how to change keyboard PC button to touch mobile (iOS, Andoird) button 1 Answer

why doesn't my touch-position conditional statements work for mobile app? 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