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 EvanCheddar · May 14, 2018 at 11:41 PM · movementbuttonssmoothtouch controlscrossplatform

How can I get touch control buttons to move player in the same way as keyboard buttons do?

Hello all. I have 2 UI buttons that move my player UP and DOWN along the Y axis. I'm using CrossPlatformInputManager.GetAxis ("Verticle"); for the touch input and movement. My question is simply how can I alter or write a script that will move my player smoothly? Perhaps inputing MathF.Larp?

Keyboard button input moves my player very smoothly and I cant seem to do the same with the touch buttons.

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 bennett_apps · May 14, 2018 at 11:44 PM 0
Share

you can use FixedUpdate(), or multiply speed by Time.deltaTime.

avatar image EvanCheddar bennett_apps · May 15, 2018 at 12:12 AM 0
Share

not sure how to implement what you're saying

avatar image bennett_apps EvanCheddar · May 15, 2018 at 12:28 AM 0
Share

Can I see your code please? I can probably implement it for you!

Show more comments

1 Reply

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

Answer by ImpOfThePerverse · May 15, 2018 at 04:52 PM

I adapted the following code from an answer I posted in another thread where someone wanted to get smooth movement when triggering a camera pan when the mouse was at either edge of the screen, but it should be re-useable here. Pass the function your calculated directionY, and where you set rb.velocity, multiply moveSpeed by the result it returns.

  public float timeToFullSpeed;
  float speedTimer;
 
   float GetSpeedMultiplier(float directionY)
  {
      if (directionY > Mathf.Epsilon)
      {
          speedTimer = Mathf.Min(timeToFullSpeed, speedTimer + Time.deltaTime);
      }
      else if (directionY < -Mathf.Epsilon)
      {
          speedTimer = Mathf.Max(-timeToFullSpeed, speedTimer - Time.deltaTime);
      }
      else
      {
          if (speedTimer > 0)
          {
              speedTimer = Mathf.Max(0f, speedTimer - Time.deltaTime)
          }
          else
          {
              speedTimer = Mathf.Min(0f, speedTimer + Time.deltaTime)
          }
      }
      return speedTimer / timeToFullSpeed;
 }
Comment
Add comment · Show 3 · 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 EvanCheddar · May 15, 2018 at 05:00 PM 0
Share

hey man, not sure how to implement your code into my code. Can you possibly take a look at my code here:

float directionY; public float moveSpeed; Rigidbody2D rb;

     void Start () {
         rb = GetComponent<Rigidbody2D> ();
     }
 
     void Update () {
         directionY = CrossPlatformInput$$anonymous$$anager.GetAxis ("Verticle");
     }
 
     void FixedUpdate()
     {
         transform.position = new Vector3(transform.position.x, 
                 $$anonymous$$athf.Clamp( transform.position.y, -3.479f, 7.25f), 
                 transform.position.z);
 
         rb.velocity = new Vector2(rb.velocity.x, directionY * moveSpeed);
     }
 
 }


avatar image ImpOfThePerverse EvanCheddar · May 15, 2018 at 05:06 PM 0
Share

This should do it. Just copy/paste the function and variables I posted into your class, and modify your code as follows:

      void Start () {
          rb = GetComponent<Rigidbody2D> ();
      }
  
      void Update () {
          directionY = CrossPlatformInput$$anonymous$$anager.GetAxis ("Verticle");
      }
  
      void FixedUpdate()
      {
          transform.position = new Vector3(transform.position.x, 
                  $$anonymous$$athf.Clamp( transform.position.y, -3.479f, 7.25f), 
                  transform.position.z);

          // calculate the speed multiplier here from directionY
          float speed$$anonymous$$ultiplier = GetSpeed$$anonymous$$ultiplier(directionY);

          // use the speed$$anonymous$$ultiplier in place of directionY
          rb.velocity = new Vector2(rb.velocity.x, speed$$anonymous$$ultiplier * moveSpeed);
      }

      // paste in the GetSpeed$$anonymous$$ultiplier code here
      float GetSpeed$$anonymous$$ultiplier... 
  }
 
 
 

avatar image EvanCheddar ImpOfThePerverse · May 15, 2018 at 05:16 PM 0
Share

dude, thank you SO much, this worked perfectly!

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

131 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

Related Questions

CrossplatformInput not detecting MultiTouch on Android build 1 Answer

Smooth Camera Movement script with problem. 0 Answers

store the user touch input 0 Answers

Move along grid where facing 1 Answer

Change in position going up not smooth 1 Answer


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