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 NyteGames · Jun 04, 2013 at 11:09 PM · c#androidiphonetouchcharacter controller

Character controller code acting differently on android to PC

I have been creating a platformer game meant for android for a few days now, due to the fact that I haven't been able to get android remote to work I have been testing on my PC (the game potentially uses on-screen buttons).

Here is my code for player movement:

 using UnityEngine;
 using System.Collections;
 
 public class playerMovement1 : MonoBehaviour {
     
     
     Rect leftButton = new Rect(0, Screen.height-(Screen.height/3), Screen.width/4, Screen.height/3);
     Rect rightButton = new Rect(Screen.width/4, Screen.height-(Screen.height/3), Screen.width/4, Screen.height/3);
     Rect jumpButton = new Rect(3*(Screen.width/4), Screen.height-(Screen.height/3), Screen.width/4, Screen.height/3);
     public float speed;
     float xV;
     float yV;
     float jumpTimeCount = 0;
     public float jumpSpeed;
     bool left;
     bool right;
     bool jump;
     bool jumping;
     bool canJump;
     public bool androidBuild;
     public bool gravity;
     public float gravityForce;
        
     // Use this for initialization
     void Start () {
     
     }
     
     void FixedUpdate(){
     
     }
     
     
     
     void Update () {
         if(jumping){
             print ("jumping");
                 jumpTimeCount += 1*Time.deltaTime;
             if(jumpTimeCount>0.8){
                 //Jumping slows as it reaches peak
                 yV = jumpSpeed*0.8f;    
             }
             //Upward movement ends when jumpTimCounter is above 1
             if(jumpTimeCount>1){
                 canJump = false;
                 jumpTimeCount = 0;
                 jumping = false;
             }
         }
         
         
     
         input ();
         tick ();
         CharacterController controller = GetComponent<CharacterController>();
         if(controller.isGrounded){
             print ("grounded");
             jumping = false;
             canJump = true;    
         }else{
                 
         }
         
         if (controller.collisionFlags == CollisionFlags.Sides){
             //Basic wall jump code
             print ("touching sides");
             canJump = true;    
         }
         if(gravity){
             controller.Move(new Vector3(0,gravityForce*Time.deltaTime,0));
         }
         
         //All movement performed here
         controller.Move(new Vector3(xV*Time.deltaTime,yV*Time.deltaTime,0));
         
     }
     
     
     void tick(){
         if(left){
             xV = -speed;
         }
         
         if(right){
             xV = speed;
         }
         
         if(!right && !left){
             xV = 0;    
         }
         
         if(jump){
             if(canJump){
                 jumping = true;
                 yV = jumpSpeed;
             
             }
         }else{
             jumping = false;    
         }
         
         if(!jumping){
             yV = 0;    
         }
     
     }
     
     void input(){
         
     
         if(Input.touches.Length>0){
     
                 
             for(int i = 0; i<Input.touchCount; i++){
             
             
                 if(jumpButton.Contains(new Vector2(Input.GetTouch(i).position.x, Screen.height-Input.GetTouch(i).position.y))){
                     jump = true;
                 }else{
                     jump = false;    
                 }
                 
         
                 if(leftButton.Contains(new Vector2(Input.GetTouch(i).position.x, Screen.height-Input.GetTouch(i).position.y))){
                     left = true;
                 }else{
                     left = false;    
                 }
                 
                 if(rightButton.Contains(new Vector2(Input.GetTouch(i).position.x, Screen.height-Input.GetTouch(i).position.y))){
                     right = true;
                 }else{
                     right = false;    
                 }
             
                 
             }
             
         }
         
         if(!androidBuild){
             if(Input.GetKey(KeyCode.A)){
                 
                 left = true;    
             }else{
                 left = false;    
             }
             if(Input.GetKey(KeyCode.D)){
                 right = true;    
             }else{
                 right = false;    
             }
             if(Input.GetKey(KeyCode.Space)){
                 jump = true;    
             }else{
                 jump = false;    
             }
         }
     }
 }

When building for android I set androidBuild to true to ensure the Keyboard input code does not interfere. When I run the game on my phone the movement seems to work differently, you don't stop when releasing the left or right button and jumping stops all other movement. Is this a problem with my code? or is this usual for android builds, and how would I go about fixing it?

Thanks

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jodon · Jun 04, 2013 at 11:56 PM

You're checking for Input.touches.Length before setting your variables back to false. You should instead set all your variables to false as the very first thing in input(), then set them to true only if your input lies inside the rect, never set them to false because they're in a loop. So let's say your first touch is "left" but your second touch isn't, now left is false again.

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 NyteGames · Jun 05, 2013 at 06:10 PM 0
Share

Thanks, this resolved the continuous movement problem, but jumping still cancels all sideways movement, can this be fixed?

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

15 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

Related Questions

InputManager vs iPhone/Android 2 Answers

2D Camera (Android, Iphone) 1 Answer

Replace Touch commands with mouse events 2 Answers

Help with iphone/android touch controls 2 Answers

Massive Touch Lag on mobile devices? 3 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