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 Ritt · Dec 31, 2015 at 01:59 AM · 2dmovementmobile2d-platformerrigidbody2d

Rigid Body Movement (Mobile Issue)

Hey guys, I'm having some troubles getting movement to work on mobile. It works on the computer but not the phone. The code that I have for the computer is:

         //Movement arrows
         if (Input.GetKey(KeyCode.RightArrow))
             r2d.velocity = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, r2d.velocity.y, 0);
         if (Input.GetKey(KeyCode.LeftArrow))
             r2d.velocity = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, r2d.velocity.y, 0);

That works, and here the code for the mobile that for some reason won't move the object:

             if (Input.touchCount > 0)
             {
                 var touch = Input.GetTouch(0);
                 if (touch.position.x < Screen.width / 2)
                 {
                     r2d.velocity = new Vector2(Input.GetAxis("Horizontal") * moveSpeed, r2d.velocity.y);
                 }
                 else if (touch.position.x > Screen.width / 2)
                 {
                     r2d.velocity = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, r2d.velocity.y, 0);
                 }
             }
     

If I do tranform.Translate instead for the touch part it works, but that removes the smoothness of the movement. If you guys have any idea let me know! Thanks a lot in advance. :-)

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 NGC6543 · Dec 31, 2015 at 04:58 AM 1
Share

The mobile does not support keyboard input so you should change Input.GetAxis unless you are using a gamepad : r2d.velocity = new Vector2(Input.GetAxis("Horizontal") * moveSpeed, r2d.velocity.y);

to something like this :

 r2d.velocity = new Vector2(SO$$anonymous$$EVALUEHERE* moveSpeed, r2d.velocity.y);

you have to check left or right yourself, and use $$anonymous$$athf.Lerp if you want to increment the value smoothly. Cheers!

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by rajan4uto · Dec 31, 2015 at 09:38 AM

Instead of "Input.GetAxis("Horizontal")" use 1 0r -1 according to your script. I mean "Input.GetAxis("Horizontal")" return 1 or -1(if you press left then it return -1 and vice-versa).

I have seen that you are working for mobile so i want to tell you one more thing that don't use rigidbody with gravity on better u should make your own code for physics or you can use "character controller" that will work fine in mobile. Main problem is that "graphics and physics are not synchronize" that's the reason in mobile, rigidbody look like it is vibrating if your camera is following it.

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
avatar image
0

Answer by _Yash_ · Dec 31, 2015 at 06:11 AM

Try Replacing:

 r2d.velocity = new Vector2(Input.GetAxis("Horizontal") * moveSpeed, r2d.velocity.y);

With:

 r2d.velocity = new Vector2((touch.position.x - Screen.width / 2) * moveSpeed, r2d.velocity.y);

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
avatar image
0

Answer by rajan4uto · Dec 31, 2015 at 06:55 AM

 public float moveSpeed=6;
     public Rigidbody r2d;
     void Update()
     {
         if (Input.touchCount > 0) {
             Debug.Log("yes");
             var touch = Input.GetTouch (0);
             if (touch.position.x < Screen.width / 2) {
                 r2d.velocity = new Vector2 (-1 * moveSpeed, r2d.velocity.y);
             } else if (touch.position.x > Screen.width / 2) {
                 r2d.velocity = new Vector3 (1 * moveSpeed, r2d.velocity.y, 0);
             }
             else
             {
                 r2d.velocity = new Vector3 (0 * moveSpeed, r2d.velocity.y, 0);
             }
         }
     }


I found this as great one because Input.GetAxis("Horizontal") return 1 or -1 by pressing left or right key so in first case use -1 and in second 1. It will work.

Comment
Add comment · Show 1 · 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 Zoelovezle · Dec 31, 2015 at 08:23 AM 0
Share

You could create a two small transparent buttons using GUI or canvas and if they are pressed that is on mobile if the buttons are touched player move.It may work more ease

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

53 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

Related Questions

Runner 2D: Move Camera or Objects? 1 Answer

Why Rigidbody 2d doesnt stop sliding? 2 Answers

Why isn't my joystick code working? 1 Answer

Mobile Movement Choppy in 2d endless runner 0 Answers

Does anyone know of a way to assign an Axis or KeyCode to a GUI Button or Texture? I'm trying to make a 2D platformer on a mobile device. As you can tell, I have no Idea what I'm doing... 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