Search Unity

Adapting Mobile Script to Have Jump

Discussion in '2D' started by VFranklin, Oct 23, 2019.

  1. VFranklin

    VFranklin

    Joined:
    Jul 26, 2017
    Posts:
    48
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Buttons_Movement : MonoBehaviour
    6. {
    7.     public float moveSpeed = 200;
    8.     public GameObject character;
    9.  
    10.     private Rigidbody2D charBody;
    11.     private float SceenWidth;
    12.  
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         SceenWidth = Screen.width;
    18.         charBody = character.GetComponent<Rigidbody2D>();
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         int i = 0;
    25.         while(i< Input.touchCount)
    26.         {
    27.             if(Input.GetTouch (i).position.x > SceenWidth / 2)
    28.             { RunCharacter(1.0f); }
    29.             if(Input.GetTouch (i).position.x< SceenWidth / 2)
    30.             { RunCharacter(-1.0f); }
    31.             i++;
    32.         }
    33.     }
    34.  
    35.     void FixedUpdate()
    36.     {
    37.     #if UNITY_EDITOR
    38.         RunCharacter(Input.GetAxis("Horizontal"));
    39.     #endif
    40.     }
    41.  
    42.     private void RunCharacter(float horizontalInput)
    43.     {
    44.         charBody.AddForce(new Vector2(horizontalInput * moveSpeed * Time.deltaTime, 0));
    45.     }
    46. }
    47.  
    I used this tutorial.


    I adapted it to have an upward button for jump. I'm confused as to how to adapt this script for an additional button on top for jumping.

    I'm sure I'll need something like private void RunCharacter(float verticalInput),
    and I'll have to change the Update method and screenWidth to account for the up button. Any advice? I don't quite understand the math behind the script. Any help would be greatly appreciated. Thank you!
     

    Attached Files:

  2. VFranklin

    VFranklin

    Joined:
    Jul 26, 2017
    Posts:
    48
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Buttons_Three : MonoBehaviour
    6. {
    7.     public float moveSpeed = 200;
    8.     public GameObject character;
    9.  
    10.     private Rigidbody2D charBody;
    11.     private float ScreenWidth;
    12.     private float ScreenHeight;
    13.  
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         ScreenWidth = Screen.width;
    19.         ScreenHeight = Screen.height;
    20.         charBody = character.GetComponent<Rigidbody2D>();
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.         int i = 0;
    27.         while (i < Input.touchCount)
    28.         {
    29.             if ((Input.GetTouch(i).position.x > ScreenWidth / 2)&&(Input.GetTouch(i).position.y < ScreenHeight / 2))
    30.             { RunCharacter(1.0f); }
    31.             if (Input.GetTouch(i).position.x < ScreenWidth / 2 && (Input.GetTouch(i).position.y < ScreenHeight / 2))
    32.             { RunCharacter(-1.0f); }
    33.             if(Input.GetTouch(i).position.y > ScreenHeight / 2)
    34.             { RunCharacterVertical(1.0f); }
    35.             i++;
    36.         }
    37.     }
    38.  
    39.     void FixedUpdate()
    40.     {
    41. #if UNITY_EDITOR
    42.         RunCharacter(Input.GetAxis("Horizontal"));
    43.         RunCharacter(Input.GetAxis("Vertical"));
    44. #endif
    45.     }
    46.  
    47.     private void RunCharacter(float horizontalInput)
    48.     {
    49.         charBody.AddForce(new Vector2(horizontalInput * moveSpeed * Time.deltaTime, 0));
    50.     }
    51.  
    52.     private void RunCharacterVertical(float verticalInput)
    53.     {
    54.         //charBody.AddForce(new Vector2(verticalInput * moveSpeed * Time.deltaTime, 0));
    55.         charBody.AddForce(Vector2.up * 100);
    56.     }
    57. }
    58.  
    I tried this but I still can't jump...
     
  3. Ledesma099

    Ledesma099

    Joined:
    May 15, 2018
    Posts:
    26
    You never call the RunCharacterVertical function in the FixedUpdate function. But first do a revision. So...
    I already saw the tutorial. First, did you set the gravity scale to 0 on the rigidbody2d component as the video?

    If you do this Then, when you apply AddForce on the Y axis, your character will fly and nothing will stop it.

    First you need to set the gravity scale again to 1. Second, you need a ground with a collider like a box, capsule, polygon, etc. To stop the character and not fall forever.

    For code:
    Code (CSharp):
    1.  
    2. // You can do this to simplify the RunCharacter method a bit.
    3. private void RunCharacter(float h) => charBody.AddForce(charBody.transform.right * h * moveSpeed); // It's the same as before. But in this case I use .transform.right because this returns the red axis of the transform in world space. I deleted Time.deltaTime. Because I don't see it necessary.
    4.  
    5. // Call Jump instead of RunCharacterVertical.
    6. private void Jump() => charBody.AddForce(charBody.transform.up * forceJump);
    7.  
    Now you must be careful when calling the jump method. Because watching the loop. I think your character will jump while you keep touching the button.

    I never made a mobile game. So this is all I can help.

    Good luck on your project.

    If you have any questions, feel free to ask. I will try to answer in what I can help
     
    Last edited: Oct 23, 2019