Have a UI Button do what my Input.GetButtonDown("Use") does?
Hey, I have been working on this for days and can't find anything on this topic. I am switching my game from keyboard controls to Mobile UI Button controls. Everything works perfectly, I have a virtual joystick that works on touch, my jump button works and my attack button works. But I can't get my "use" button to work.
My use input has the keyboard input "f" assigned. So when I press "f" next to a switch it drops a box, or if I press "f" on a level door, I enter that level door. That is because I am using Input.GetButtonDown("Use"); in several IF statements throughout my various scripts.
Now I am trying to replace that pressing "f" or Use buttondown with a UI Button. This seems to be a really easy thing, but it is NOT. I know I can attach a trigger event to my button and do the "Pointer Down (Base Event Data)", as that is what I have done with my jump and attack functions. But it doesn't work with my Use input.
Basically I want this UI Button to have the same effect as pressing the letter "f" on the keyboard. Here is a code example of how I use the letter "f" when I am next to a switch.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class FallingObject : MonoBehaviour {
public GameObject fallingObject;
private LevelManager theLevelManager;
public bool waitingForRespawn;
public SpriteRenderer leverStart;
public Sprite leverMiddle;
public Sprite leverRight;
public AudioSource leverActivation;
//new try for trigger and button
private bool triggerEntered;
// Use this for initialization
void Start () {
theLevelManager = FindObjectOfType<LevelManager> ();
}
// Update is called once per frame
void Update () {
if (theLevelManager.respawnCoActive) {
fallingObject.GetComponent<Rigidbody2D> ().isKinematic = true;
waitingForRespawn = true;
}
if (waitingForRespawn && !theLevelManager.respawnCoActive) {
waitingForRespawn = false;
leverStart.sprite = leverMiddle;
}
if (Input.GetButtonDown("Use") && triggerEntered == true) {
fallingObject.GetComponent<Rigidbody2D> ().isKinematic = false;
leverStart.sprite = leverRight;
leverActivation.Play ();
}
}
public void useButtonClick(){
Debug.Log ("the button has been clicked");
}
//this decides if the player is in the zone of the lever
void OnTriggerStay2D(Collider2D other) {
if(other.tag == "Player"){
triggerEntered = true;
}
}
void OnTriggerExit2D(Collider2D other){
if (other.tag == "Player") {
triggerEntered = false;
}
}
}
Your answer
Follow this Question
Related Questions
With the new unity input system. How do I setup UI buttons like in the old system. 0 Answers
How can I generate and setup buttons via code 1 Answer
How do I change a state in an FSM with an UI button? 0 Answers
UI button events not working on android 3 Answers
using a button witout pressing it ? 1 Answer