Search Unity

Re-coding movement script for android?

Discussion in 'Scripting' started by MitjaHD, Nov 14, 2018.

  1. MitjaHD

    MitjaHD

    Joined:
    Sep 30, 2018
    Posts:
    64
    Hey there i've created a simple platformer game and I'm wondering if there is an easy way to make it work on android as well. I haven't created any android apps yet, but I've tested and imported some old projects onto the android, however control obviously don't work.

    Code (CSharp):
    1. void Update()
    2.     {
    3.         IsGrounded = rb.IsTouching(filter);                
    4.  
    5.         if (Input.GetButton("Jump") && IsGrounded)          
    6.         {
    7.             rb.velocity = new Vector2(rb.velocity.x, 0f);   //sila na y osi je 0  
    8.             jump = true;                                  
    9.         }
    10.     }
    Code (CSharp):
    1.  void FixedUpdate() {
    2.  
    3.         //**MOVEMENT**
    4.         move = Input.GetAxisRaw("Horizontal");
    5.    
    6.         if (rb.velocity.y>0)                  
    7.         {
    8.             move = move / 2;                    
    9.         }
    10.        
    11.         movement = new Vector2(move, 0f);      
    12.  
    13.         rb.AddForce(movement * speed);        
    14.  
    15.         Vector2 speedNew = rb.velocity;        
    16.         if (speedNew.x > maxSpeed)            
    17.         {
    18.             speedNew.x = maxSpeed;            
    19.             rb.velocity = speedNew;            
    20.         }
    21.         else if (speedNew.x < -maxSpeed)      
    22.         {
    23.             speedNew.x = -maxSpeed;
    24.             rb.velocity = speedNew;
    25.         }
    26.  
    27.         //**JUMP**//
    28.         if (jump)
    29.         {
    30.            
    31.             rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);          
    32.             rb.velocity = new Vector2(rb.velocity.x / 1.5f, rb.velocity.y);    
    33.             GetComponent<AudioSource>().Play();                                
    34.             jump = false;                                                    
    35.         }
    So if i'm not incorrect I need to get input via touches and not via keyboard, but I'm not really sure how I could do this and maybe it's not as easy as I think. I would have to create 3 buttuns as well I guess.
     
  2. Reeii

    Reeii

    Joined:
    Feb 5, 2016
    Posts:
    91
    As you already said, in theory you only have to recode the input detection. The easiest way to do this would be a button for each input action (with the built-in Button component. The only thing to do then is creating a public method for each button that will be called when the button is pressed (you have to set the method in the Button component then). Such a method could look like this:
    Code (CSharp):
    1. public void Jump() {
    2.         if (IsGrounded)        
    3.         {
    4.             rb.velocity = new Vector2(rb.velocity.x, 0f);   //sila na y osi je 0
    5.             jump = true;                                
    6.         }
    7. }
    But depending on the game, you would want to use types of input like e.g. a joystick. For this, I'd recommend taking a look at the IDragHandler (https://docs.unity3d.com/ScriptReference/EventSystems.IDragHandler.html) and IEndDragHandler (https://docs.unity3d.com/ScriptReference/EventSystems.IEndDragHandler.html), if you want to code it by yourself. Otherwise there will surely be some script on the internet or you could use the one that is in the Standard Assets (i think there is one).
     
  3. MitjaHD

    MitjaHD

    Joined:
    Sep 30, 2018
    Posts:
    64
    Yeah I've looked into buttons, however If I hold down the button it only returns one thing, it's not updating every frame and checking for input, so my jumping doesn't work correctly, because I want the player to jump even if you're just holding down the button (and you're grounded).
     
  4. MitjaHD

    MitjaHD

    Joined:
    Sep 30, 2018
    Posts:
    64
    Alright I fixed my own problem by changing the code, nevermind.