Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Android FPS Control Script

Discussion in 'Android' started by raahilsha, Jun 30, 2013.

  1. raahilsha

    raahilsha

    Joined:
    Feb 12, 2013
    Posts:
    11
    Hi Guys,

    I just made an FPS Control Script for Android from scratch, and I have decided to release it to the forums for people who want to use it. What's special about this one from the Unity default one is threefold:

    1) The move pad is "floating," which means that its position is determined by where ever you press.
    2) Rotation uses a full half of the screen, instead of being confined to a rotation pad.
    3) There is now (togglable) jumping and double-jumping

    Here is a screenshot of the setup:
    $setup.png

    And here is the script:

    Code (csharp):
    1.  
    2.  
    3. #pragma strict
    4.  
    5. var cameraPivot : Transform;
    6.  
    7. var speed : float = 4;
    8.  
    9. private var thisTransform : Transform;
    10. private var character : CharacterController;
    11. private var cameraVelocity : Vector3;
    12. private var velocity : Vector3;
    13. private var canJump = true;
    14.  
    15. var movementOriginX : float;
    16. var movementOriginY : float;
    17.  
    18. function Start ()
    19. {
    20.     thisTransform = GetComponent(Transform);
    21.     character = GetComponent(CharacterController);
    22.     originalRotation = transform.rotation.eulerAngles.y;
    23.     movePad.transform.position = new Vector2(-1,-1);
    24.     moveOutline.transform.position = new Vector2(-1,-1);
    25.     jump = false;
    26.     doubleJump = false;
    27. }
    28.  
    29. function Update ()
    30. {
    31.     var moveDiff : Vector2;
    32.     for (var touch : Touch in Input.touches)
    33.     {
    34.         if (touch.phase == TouchPhase.Began)
    35.         {
    36.             if (jumpButton.HitTest(touch.position))
    37.             {
    38.                 jump = true;
    39.             }
    40.             else if (touch.position.x < Screen.width / 2)
    41.             {
    42.                 leftFingerID = touch.fingerId;
    43.                 leftFingerCenter = touch.position;
    44.                 moveOutline.transform.position.x = touch.position.x / Screen.width;
    45.                 moveOutline.transform.position.y = touch.position.y / Screen.height;
    46.                 movePad.transform.position.x = touch.position.x / Screen.width;
    47.                 movePad.transform.position.y = touch.position.y / Screen.height;
    48.             }
    49.             else
    50.             {
    51.                 rightFingerID = touch.fingerId;
    52.             }
    53.         }
    54.         else if (touch.phase == TouchPhase.Moved)
    55.         {
    56.             if (touch.position.x < Screen.width / 2)
    57.             {
    58.                 if (leftFingerID == touch.fingerId)
    59.                 {
    60.                     mDiff = touch.position - leftFingerCenter;
    61.                     var distPer = mDiff.magnitude * 100 / moveStickDiff;
    62.                     if (distPer > 100)
    63.                     {
    64.                         distPer = 100;
    65.                     }
    66.                     leftFingerInput = mDiff.normalized * distPer / 100;
    67.                    
    68.                     movePad.transform.position.x = leftFingerCenter.x / Screen.width + mDiff.normalized.x * distPer / 100 * moveStickDiff / Screen.width;
    69.                     movePad.transform.position.y = leftFingerCenter.y / Screen.height + mDiff.normalized.y * distPer / 100 * moveStickDiff / Screen.height;
    70.                 }
    71.             }
    72.             else
    73.             {
    74.                 if (rightFingerID == touch.fingerId)
    75.                 {
    76.                     rightFingerInput = touch.deltaPosition * Time.smoothDeltaTime;
    77.                 }
    78.             }
    79.         }
    80.         else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
    81.         {
    82.             if (touch.fingerId == leftFingerID)
    83.             {
    84.                 movePad.transform.position = new Vector2(-1,-1);
    85.                 moveOutline.transform.position = new Vector2(-1,-1);
    86.                 leftFingerID = -1;
    87.                 leftFingerInput = new Vector2(0, 0);
    88.             }
    89.             if (touch.fingerId == rightFingerID)
    90.             {
    91.                 rightFingerID = -1;
    92.                 rightFingerInput = new Vector2(0, 0);
    93.             }
    94.         }
    95.     }
    96.    
    97.     rotationX += rightFingerInput.x * 25;
    98.     rotationY += rightFingerInput.y * 25;
    99.     rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
    100.     transform.rotation =  Quaternion.Slerp (transform.rotation, Quaternion.Euler(0, originalRotation + rotationX, 0),  0.1);
    101.     cameraPivot.localRotation =  Quaternion.Slerp (cameraPivot.localRotation, Quaternion.Euler(cameraPivot.localRotation.x-rotationY, 0, 0),  0.1);
    102.    
    103.     moveDirection = thisTransform.TransformDirection(new Vector3(leftFingerInput.x, 0, leftFingerInput.y));
    104.     moveDirection *= speed;
    105.     moveDirection += Physics.gravity;
    106.    
    107.     if (character.isGrounded)
    108.     {
    109.         doubleJump = false;
    110.         if (jump  jumpingEnabled)
    111.         {
    112.             velocityJ = character.velocity / 3;
    113.             velocityJ.y = jumpSpeed;
    114.         }
    115.         else
    116.         {
    117.             velocityJ = new Vector3(0, 0, 0);
    118.         }
    119.     }
    120.     else
    121.     {
    122.         if (!doubleJump  jump  doubleJumpingEnabled)
    123.         {
    124.             velocityJ = character.velocity / 3;
    125.             velocityJ.y = jumpSpeed;
    126.             doubleJump = true;
    127.         }
    128.         velocityJ.y += Physics.gravity.y * Time.smoothDeltaTime;
    129.     }
    130.    
    131.     moveDirection += velocityJ;
    132.    
    133.     character.Move(moveDirection * Time.smoothDeltaTime);
    134.     jump = false;
    135. }
    136.  
    137. var rightFingerID;
    138. var leftFingerID;
    139. var leftFingerCenter : Vector2;
    140. var mDiff : Vector2;
    141. var moveStickDiff = 100;
    142. var leftFingerInput : Vector2;
    143. var rightFingerInput : Vector2;
    144.  
    145. var moveOutline : GUITexture;
    146. var movePad : GUITexture;
    147. var jumpButton : GUITexture;
    148.  
    149. var rotationX : float;
    150. var rotationY : float;
    151. var minimumY = -20;
    152. var maximumY = 20;
    153.  
    154. var originalRotation : float;
    155. var moveDirection : Vector3;
    156. var jump : boolean;
    157. var doubleJump : boolean;
    158.  
    159. var jumpSpeed : float = 25;
    160. var velocityJ : Vector3;
    161. var doubleJumpingEnabled : boolean = true;
    162. var jumpingEnabled : boolean = true;
    163.  
    164.  


    Enjoy!
     
    Last edited: Jun 30, 2013
    JorgeAmVF, spiningit and carmel399 like this.
  2. niko-belic

    niko-belic

    Joined:
    Sep 1, 2012
    Posts:
    67
    I tested on my Acer Iconia a 500 and I have a little problem
    Move pad has no effect on the Move Outline.
    GUI textures are not refundable. Move Outline texture is moving very quickly and is off screen
     
    carmel399 likes this.
  3. raahilsha

    raahilsha

    Joined:
    Feb 12, 2013
    Posts:
    11
    Movepad is supposed to be a 50x50 circle. Moveoutline is supposed to be a 100x100 outline. The outline stays still while the movepad moves with the finger. I still need to write code to constrain the pad to the outline.
     
    carmel399 likes this.
  4. raahilsha

    raahilsha

    Joined:
    Feb 12, 2013
    Posts:
    11
    I have an Acer A500 and it works fine. Just try to keep playing with it.
     
  5. niko-belic

    niko-belic

    Joined:
    Sep 1, 2012
    Posts:
    67
    The outline is now ok.
    Movepad is moving outside the ring.
    Cancellation touch Movepad not to ring back
     
  6. raahilsha

    raahilsha

    Joined:
    Feb 12, 2013
    Posts:
    11
    I have fixed it. Updating original post right now.
     
  7. raahilsha

    raahilsha

    Joined:
    Feb 12, 2013
    Posts:
    11
    Added double jumping and jumping. You need to make a jump button.
     
  8. niko-belic

    niko-belic

    Joined:
    Sep 1, 2012
    Posts:
    67
    Really good work
    after the update is better.
    it would need fix a small thing ..

    What is a license to use?
     
    carmel399 likes this.
  9. raahilsha

    raahilsha

    Joined:
    Feb 12, 2013
    Posts:
    11
    It is free to use for both commercial and free products. I made this for fun, so I am giving it to the community for free. After all, I've gotten most of my help from previous forum posts!
     
    JorgeAmVF likes this.
  10. Zaddo67

    Zaddo67

    Joined:
    Aug 14, 2012
    Posts:
    489
    Thank you very much. This will certainly come in handy for a future project.
     
  11. nbg_yalta

    nbg_yalta

    Joined:
    Oct 3, 2012
    Posts:
    378
    can you pack .unitypackage with example pls.
     
  12. DDimitrovD

    DDimitrovD

    Joined:
    Nov 4, 2013
    Posts:
    1
    Thank you mate. I am starting my first experiences with android on Unity and I will study the script...
     
  13. Halo500

    Halo500

    Joined:
    Apr 24, 2013
    Posts:
    37
    Hello raahilsha! I really dislike the mobile control sticks unity gives you in the mobile standard assets. I've been looking everywhere for a floating move pad and yours looks promising. I tried to test your script out on my game on my galaxy s4 but my game crashes the instant I touch the screen. If I press the back button, the game actually pauses.

    Any idea what I might have done wrong?
     
  14. Edyvargas

    Edyvargas

    Joined:
    Dec 2, 2013
    Posts:
    3
    Hi, thaks for share this great script for android developing, but somehow i cant get this to work:

    I create two GUI textures on my project, one its named Pad and the other Pad Outline (put 2 images for the Gui textures one 50x50 and the other 100x100 px), then place the FirstPersonSetup from the Standard Assets (mobile) on my project, then deactivate the First Person Control Script from the "Player" on the First Person Controls Prefab in the game (as in the example image) and deactivate the Dual TouchPads from the game, then attach the new My FPSMovement script to the "Player" and drag the Gui textures from the game to the new script, but when i test the game on a Nexus 7 the controls dont shows up and nothing happens..

    Thanks in advance for any guide on this, im back to unity after several months and perhaps im doing something wrong...:confused:
     
    Last edited: Dec 2, 2013
  15. HerewithLT

    HerewithLT

    Joined:
    Feb 1, 2014
    Posts:
    1
    Can you add to this movingplatform? ;/ Bicose i don't cnow how to make lift for this playr :/
     
  16. MDragon

    MDragon

    Joined:
    Dec 26, 2013
    Posts:
    329
    ...Dang, I was actually going to look into this soon! Dead Trigger 2's controls were, in a way, inspiring and showed how overly simple and distracting my joystick controls are. This'll save me some time down the road.

    And I guess it's going to be "fun" converting this to C# then dissecting piece by piece as it flies over my head and I try to edit it even further ;)

    Thanks.
     
  17. zafadadynamic

    zafadadynamic

    Joined:
    Oct 21, 2013
    Posts:
    4
    Hi, everyone :)

    Thanks for this. It actually put me on the right track and I was able to make simple controls using the GUI. I have a better idea now though which is, sigh, so darn revolutionary. I'll keep you all updated if you're interested.

    Oh, and I'm kind of 'new' here so sorry for bringing up an old thread.
     
  18. montana2008

    montana2008

    Joined:
    Sep 12, 2013
    Posts:
    1
    Any chance you could help me with this. i cant get it working, im newbie with unity...any chance of a package? so i can look at it.
     
  19. Shukerullah

    Shukerullah

    Joined:
    Mar 15, 2013
    Posts:
    97
    Thankyou very much :)
     
  20. RASKALOF

    RASKALOF

    Joined:
    Jul 9, 2012
    Posts:
    56
    This is the best and easy mobile controls what i ever seen! Thanx you! You saved my time a lot!
     
  21. carmel399

    carmel399

    Joined:
    Oct 27, 2014
    Posts:
    1
    is license is freeware available?
     
  22. Xinerki

    Xinerki

    Joined:
    Feb 1, 2015
    Posts:
    2
    When I start up the game the movepad and moveoutline just dissapear..
     
  23. shortyzsly

    shortyzsly

    Joined:
    Nov 15, 2013
    Posts:
    1
    Never mind the parsing error. Got it sorted. however now it says it expected a normal close bracket ) on the (jump jumpingEnabled) line between jump and jumpEnabled. not sure why. Script is fine.....
     
  24. tonno16

    tonno16

    Joined:
    Feb 7, 2015
    Posts:
    3
    I know is old thread, but...I tried to use this code. Unity give me some errore code in this script. Is possible?
     
  25. Noxury

    Noxury

    Joined:
    Mar 8, 2015
    Posts:
    22
    @shortyzsly and @tonno16:
    try adding "&&" between the if-statements in line 108/120

    However, this script is outdated since GUI-Textures are replaced with the new UI-System.
     
    Last edited: Jul 18, 2015
  26. darktecnicalgamer

    darktecnicalgamer

    Joined:
    Jul 1, 2020
    Posts:
    1
    Hi, can you tell me how can I add this script in the fps game which is given as learn that fps shooter game as I wanted it to play in android?