• 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 Johnz1234 · Apr 06, 2015 at 04:32 PM · gamescriptingbasicsmobile devices

How to make mobile movement

hello guys iam begginer in unity this is my code for my 2d ball game i want to make for mobile to move with there gui buttons left and right and jump button any idea how to do it ? every one is saying to me to start simple so iam making simplest game ball to move and jump its working in pc now i wanted to make it for mobile..

here is my code can someone tell me how to make it for mobile? using UnityEngine; using System.Collections;

public class PlayerController : MonoBehaviour {

 public float moveSpeed;
 public float jumpHeight;

 public Transform groundCheck;
 public float groundCheckRadius;
 public LayerMask whatIsGround;
 private bool grounded;

 private bool doubleJumped;

 private Animator anim;

 void Start () {
     anim = GetComponent<Animator> ();
 }

 void FixedUpdate()
 {
     grounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, whatIsGround);
 }

 void Update ()
 {
     if (grounded)
         doubleJumped = false;


     if (Input.GetKeyDown (KeyCode.Space) && grounded)
     {
         //GetComponent<Rigidbody2D>().velocity = new Vector2 (GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
         Jump ();
     }

     if (Input.GetKeyDown (KeyCode.Space) && !doubleJumped && !grounded)
     {
         //GetComponent<Rigidbody2D>().velocity = new Vector2 (GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
         Jump ();
         doubleJumped = true;
     }

     if(Input.GetKey (KeyCode.D))
     {
         GetComponent<Rigidbody2D>().velocity = new Vector2 (moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
     }

     if(Input.GetKey (KeyCode.A))
     {
         GetComponent<Rigidbody2D>().velocity = new Vector2 (-moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
     }

     anim.SetFloat ("Speed", Mathf.Abs(GetComponent<Rigidbody2D> ().velocity.x));
 }

 public void Jump()
 {
     GetComponent<Rigidbody2D>().velocity = new Vector2 (GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
 }

}

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 ozturkcompany · Apr 06, 2015 at 04:42 PM 0
Share

You can start by learning how to use Input.touches array. Example is on script reference.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Dreceon · Apr 07, 2015 at 09:55 AM

The easiest way to do this without coding would be to buy the Control Freak asset from the asset store, with that you can just add buttons, check GetKey, enter the key that corresponds to the button and simply rename every "Input" with "CFInput" in your script.

Also check out this Unity3d question, it's similar to yours. http://answers.unity3d.com/questions/796506/how-to-translate-key-input-controls-to-touch-contr.html

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 SirBoboHobo · Apr 06, 2015 at 08:31 PM

Adding Mobile Control

You can have a look at this guy to learn a bit, this is more intermediate, but learn the basics first (not hard to find) and watch some tutorials like this one, or search on YouTube, there are tons of them

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

22 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

Related Questions

problem with the change's speed between 2 texture using a switch script, how can i speed it up? 1 Answer

How do you activate a script upon play? 3 Answers

getting udp package info inside unity (GlovePIE) 0 Answers

How to mute my volume 2 Answers

Mobile Game: Setup right confiner size depending by device 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