im very new to script....need help with a 2.5 d mobile game

im trying to make a 2.5d style mobile style game. my character works on my PC put not everything works for mobile; the firing to be exact. i understand that i need to make a separate function but every time i do it stops working…could a more experienced person tell me what i need to fix…i supply don’t know what to do.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class fireBullet : MonoBehaviour {

public float timeBetweenBullets = 0.15f;
public GameObject projectile;

//bullet info
public Slider playerAmmoSlider;
public int maxRounds;
public int startingRounds;
int remainingRounds;

float nextBullet;

//graphic info
public Sprite weaponSprite;
public Image weaponImage;

// Use this for initialization
void Awake () {
nextBullet = 0f;
remainingRounds = startingRounds;
playerAmmoSlider.maxValue = maxRounds;
playerAmmoSlider.value = remainingRounds;

}

// Update is called once per frame
void Update () {

playerController myPlayer = transform.root.GetComponent ();

if (Input.GetAxisRaw (“Fire 1”) > 0 && nextBullet < Time.time && remainingRounds>0) {
nextBullet = Time.time + timeBetweenBullets;
Vector3 rot;
if (myPlayer.GetFacing () == -1f){
rot = new Vector3 (0, -90, 0);
} else rot = new Vector3 (0, 90, 0);
Instantiate (projectile, transform.position, Quaternion.Euler (rot));

remainingRounds -=0;
playerAmmoSlider.value = remainingRounds;
}
}

public void reload(){
remainingRounds = maxRounds;
playerAmmoSlider.value = remainingRounds;
}

public void initializeWeapon(){
nextBullet = 0;
playerAmmoSlider.maxValue = maxRounds;
playerAmmoSlider.value = remainingRounds;

}

}

for mobile do you have some fire-button on screen, or how the player is shooting?

I do have a button on screen to fire… Like I said I do understand that I need a separate function so the button works but I don’t understand how I would go about that… because my code is in the update…I have tried putting it in a function…I know I’m doing it wrong that’s why I ask

could try something like this,

in your UI button

  • Add event trigger component
  • Add events Button Down and Button Up
    3221466--246966--upload_2017-9-15_21-36-44.png
  • For the reference field, drag your gameobject with the firebullet script
  • Add these ButtonDown() and ButtonUp() functions to that script
  • Then from that dropdown list, select FireBullet.ButtonUp and ButtonDown, so they get called when button is up or down
  • After that could add some boolean there, when ButtonDown is called, its true and ButtonUp its false, and make firing happen in Update if the boolean is true
using UnityEngine;

public class ButtonMobile : MonoBehaviour
{
    public void ButtonDown()
    {
        Debug.Log("ButtonDown");
    }

    public void ButtonUp()
    {
        Debug.Log("ButtonUp");
    }
}
1 Like