Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Help Wanted Getting this code running in android ?

Discussion in 'Editor & General Support' started by crystalbajgai, Jan 24, 2021.

  1. crystalbajgai

    crystalbajgai

    Joined:
    Jan 24, 2021
    Posts:
    2
    I am new to unity and idk much about programming so I wanna know the way of getting this code below to make work in android or touchscreen. I want to control this in touch screen in android :/
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3.  
    4. public class PlayerMovement : MonoBehaviour {
    5.  
    6.     // This is a reference to the Rigidbody component called "rb"
    7.     public Rigidbody rb;
    8.  
    9.     public float forwardForce = 2000f;    // Variable that determines the forward force
    10.     public float sidewaysForce = 500f;  // Variable that determines the sideways force
    11.  
    12.     // We marked this as "Fixed"Update because we
    13.     // are using it to mess with physics.
    14.     void FixedUpdate ()
    15.     {
    16.         // Add a forward force
    17.         rb.AddForce(0, 0, forwardForce * Time.deltaTime);
    18.  
    19.         if (Input.GetKey("d"))    // If the player is pressing the "d" key
    20.         {
    21.             // Add a force to the right
    22.             rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    23.         }
    24.  
    25.         if (Input.GetKey("a"))  // If the player is pressing the "a" key
    26.         {
    27.             // Add a force to the left
    28.             rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    29.         }
    30.  
    31.         if (rb.position.y < -1f)
    32.         {
    33.             FindObjectOfType<GameManager>().EndGame();
    34.         }
    35.     }
    36. }
    37.  
     
  2. crystalbajgai

    crystalbajgai

    Joined:
    Jan 24, 2021
    Posts:
    2
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    12,273
    None of the above code will be useful for this because it is for keyboard.

    Check out any random tutorial on mobile touchscreen DPADs.

    You're also welcome to see some of my examples in my public proximity_buttons package.

    Check out the space shooter and the twinstick game for virtual DPADs.

    proximity_buttons is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/proximity_buttons

    https://github.com/kurtdekker/proximity_buttons

    https://gitlab.com/kurtdekker/proximity_buttons

    https://sourceforge.net/projects/proximity-buttons/
     
    crystalbajgai likes this.
unityunity