Implement UI buttons to make player shoot and jump

Hi,

Just before I begin, this game is being built for IOS (not sure if that matters, but thought I’d put it out there)

I currently have 2 UI buttons which allow my player to move left and right which work and are great. I used the Simple Input package from the assets store to do this.

Now I am trying to make my character jump and shoot.

I have watched literally hundreds of tutorials on YouTube, but none of them work for me. They involve using the CrossPlatformInput scripts on the UI buttons using event triggers etc. I have even tried just putting my player script into a button click and selecting “Jump” or “Shoot” and neither work.

I have attached my script incase it is of use if you are able to assist me. The “Jump” button is currently assigned to the Space bar, and the “shoot” is the Left Ctrl keyboard button.

Thanks,
Adam

(I used the insert code button above)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

public class Player : MonoBehaviour
{
    // Config
    [Header("Movement")]
    [SerializeField] float runSpeed = 5f;
    [SerializeField] float jumpSpeed = 5f;


    [Header("Projectile")]
    [SerializeField] GameObject bulletPrefab;
    [SerializeField] GameObject gun;
    [SerializeField] float projectileSpeed = 10f;
    [SerializeField] float projectileFiringPeriod = 0.1f;

    [Header("Player FX")]
    [SerializeField] AudioClip playerShoot;
    [SerializeField] [Range(0, 1)] float playerShootVolume = 0.7f;


    Coroutine shootingCoroutine;

    // state
    bool isAlive = true;

    // Cached Component References
    Rigidbody2D myRigidBody;
    Animator myAnimator;
    PolygonCollider2D myBodyCollider;
    BoxCollider2D myFeet;
       
    //Message then methods
    void Start()
    {
        myRigidBody = GetComponent<Rigidbody2D>();
        myAnimator = GetComponent<Animator>();
        myBodyCollider = GetComponent<PolygonCollider2D>();
        myFeet = GetComponent<BoxCollider2D>();
    }

    // Update is called once per frame
    void Update()
    {
        if (!isAlive) { return; }

        Run();
        FlipSprite();
        Jump();
        Die();
        Shoot();
    }

    public void Run()
    {
        float controlThrow = SimpleInput.GetAxis("Horizontal"); // -1 to +1
        Vector2 playerVelocity = new Vector2(controlThrow * runSpeed, myRigidBody.velocity.y);
        myRigidBody.velocity = playerVelocity;
        Debug.Log(playerVelocity);

        bool playerHasHorizontalSpeed = Mathf.Abs(myRigidBody.velocity.x) > Mathf.Epsilon;
        myAnimator.SetBool("Running", playerHasHorizontalSpeed);
    }

    public void Jump()
    {
        if (!myFeet.IsTouchingLayers(LayerMask.GetMask("Ground"))) { return; }
       
       
        if (CrossPlatformInputManager.GetButtonDown("Jump"))
        {
            Vector2 jumpVelocityToAdd = new Vector2(0f, jumpSpeed);
            myRigidBody.velocity += jumpVelocityToAdd;
        }
    }

    public void Die()
    {
        if (myBodyCollider.IsTouchingLayers(LayerMask.GetMask("Enemy", "Spikes")))
        {
            isAlive = false;
            myAnimator.SetTrigger("Die");
            FindObjectOfType<GameSession>().ProcessPlayerDeath();
        }
    }

    public void FlipSprite()
    {
        bool playerHasHorizontalSpeed = Mathf.Abs(myRigidBody.velocity.x) > Mathf.Epsilon;
        if (playerHasHorizontalSpeed)
        {
            transform.localScale = new Vector2(Mathf.Sign(myRigidBody.velocity.x), 1f);
        }
    }

    public void Shoot()
    {
        if (CrossPlatformInputManager.GetButtonDown("Fire1"))
        {
            shootingCoroutine = StartCoroutine(ShootContinuously());
        }
        if (CrossPlatformInputManager.GetButtonUp("Fire1"))
        {
            StopCoroutine(shootingCoroutine);
        }
    }

    IEnumerator ShootContinuously()
    {
        while (true)
        {
            GameObject ball = Instantiate(
                    bulletPrefab,
                    gun.transform.position,
                    Quaternion.identity) as GameObject;
            if (transform.localScale.x > 0f)
            {
                ball.GetComponent<Rigidbody2D>().velocity = new Vector2(projectileSpeed, 0);
            }
            else
            {
                ball.GetComponent<Rigidbody2D>().velocity = new Vector2(-projectileSpeed, 0);
            }
            yield return new WaitForSeconds(projectileFiringPeriod);
            AudioSource.PlayClipAtPoint(playerShoot, Camera.main.transform.position, playerShootVolume);
        }
    }
}

I’m not a huge fan of the cross platform system either, but they do work.

If you want to see a small collection of touchscreen buttons and analog joysticks, you’re welcome to dig through the various little minigames and demos in my proximity_buttons package.

proximity_buttons is presently hosted at these locations:

https://bitbucket.org/kurtdekker/proximity_buttons

https://github.com/kurtdekker/proximity_buttons