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 Skyler_Hawkins · Sep 02, 2017 at 06:14 PM · touchcontrollerhorizontal movement

Swapping Horizontal Input for Mobile

Hello,

I'm trying to port my game to mobile devices but keep running into a problem with getting my player to move at all. I have only done "runners" on mobile before and didn't have to worry about using the screen to move the player, so this is different and can't seem to see what I'm doing wrong.

I've tried several different optional I've found while searching, however none seem to work with my scripting. I don't want to have to add buttons just to move left or right and would rather just have the player swipe left/right or just be able to hold down one side or the other - either will work fine. I think at this point I just want to player to move.

The code below is the codes I have been using for my player on PC to test and not any of my attempts to move with touch controls.

Thank you for your time.

using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class PlayerController : MonoBehaviour {
 
     public bool alive;
     //Speed of the Player
     public float speed = 10.0f;
 
     //Bounds of Player
     public float leftBound = -5f;
     public float rightBound = 5f;
     // Use this for initialization
     void Start () {
         alive = true;
     }
     
     // Update is called once per frame
     void Update () {
         
         //Horizontal Speed
         float movementSpeedX = Time.deltaTime * Input.GetAxis("Horizontal") * speed;
         
         //Player Movement
         transform.Translate(movementSpeedX, 0, 0);
       
         //Creates bounds around player
         if (transform.position.x > rightBound)
         {
             transform.position = new Vector3(rightBound, transform.position.y, 0);
         }
         else if (transform.position.x < leftBound)
         {
             transform.position = new Vector3(leftBound, transform.position.y, 0);
         }
     }
 }
 
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 MacDx · Sep 04, 2017 at 06:50 PM 0
Share

What you could do is detect a touch and its position on the screen. If the touch is on the left side of the screen then move left, if it is on the right side then go right.

Pseudocode:

 if touches.Count > 0:
     Touch t = GetTouch(0)
     screenCenter = screenWidth / 2 //This would be in pixels
 
     if t.position.x > screenCenter:
         $$anonymous$$oveRight
     else:
         $$anonymous$$oveLeft

0 Replies

· Add your reply
  • Sort: 

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

91 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

Related Questions

Dual joystick Position always resets on playing 1 Answer

Unity 2D Touch Controll via on screen button for Mobile 0 Answers

Touch Event throwing XCode Error (not sure what it is - screenshot) 1 Answer

Swipe menu, problem! 0 Answers

Move object with touch 3D with script 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