- Home /
urgent, pls help. Selection through keyinput in Canvas
Right now I'm working on the Start Menu for my 2d game - I'm doing it through canvas. It's working fine if I wanted to use mousepad and click for selection, but my game is gonna be hooked up to two controllers that only take arrow keys, g and f as input.
In game the players(it's a multiplayer) controls three input keys each to move a sprite around (arrowkeys for movement and they need to both click f and g to attack).
Is it in any way possible to use this concept to click on button a in the UI? (as in the player moves a gameobject around and if it's untop a button and they do the FG click, the button is seen as clicked)
Added part of my code so you can see what is used to move the object in game:
public class simpleMove : MonoBehaviour {
Animator anim;
float timeframe = 0;
float timeframe1 = 0;
bool chkinput=false;
bool chkinput1=false;
bool checkani=false;
float speed = 5;
// Use this for initialization
void Start () {
anim = GetComponent<Animator> ();
}
public void MovementHand (){
//hand movement
var move = new Vector3 (Input.GetAxis ("Horizontal"), Input.GetAxis ("Vertical"), 0);
transform.position += move * speed * Time.deltaTime;
//x axis stop
if (transform.position.x <= -6.5f) {
transform.position = new Vector2 (-6.5f, transform.position.y);
} else if (transform.position.x >= 6.5f) {
transform.position = new Vector2 (6.5f, transform.position.y);
}
// Y axis stop
if (transform.position.y <= -5.0f) {
transform.position = new Vector2 (transform.position.x, -5.0f);
} else if (transform.position.y >= 5.0f) {
transform.position = new Vector2 (transform.position.x, 5.0f);
}
}
public void Attack (){
if (Input.GetKeyDown (KeyCode.F)) {
chkinput = true;
}
if (chkinput) {
timeframe += 1 * Time.deltaTime;
if (Input.GetKeyDown (KeyCode.G)) {
checkani=true;
timeframe = 0;
chkinput = false;
//This is where I would like to click
}
if (timeframe > 1) {
timeframe = 0;
chkinput = false;
}
}
if (Input.GetKeyDown (KeyCode.G)) {
chkinput1 = true;
}
if (chkinput1) {
timeframe1 += 1 * Time.deltaTime;
if (Input.GetKeyDown (KeyCode.F)) {
checkani=true;
timeframe1 = 0;
chkinput1 = false;
//This is where I would like to click
}
if (timeframe1 > 1) {
timeframe1 = 0;
chkinput1 = false;
}
}
}
public void OnClick(){
if (checkani) {
anim.SetTrigger ("attack_the_bug");
AudioSource sound = gameObject.GetComponent<AudioSource> ();
//or here...
sound.Play ();
checkani = false;
}
}
// Update is called once per physics timestep
void Update () {
MovementHand ();
Attack ();
OnClick ();
}
}
hope you guys can help..
I am not sure if it what you want but if you want to simulate a mouse click from code, you could use ExecuteEvents.Execute
. http://forum.unity3d.com/threads/force-a-pointer-press-on-a-button-from-code-as-if-it-came-from-a-mouse-touch-event.280131/
That way, when user press F or G, you would simulate a mouse click on the respective button.
Again I am not sure if it is what you want which is why i posted it as a comment. Hope this help!
from what other people are writing about it, this seems to be what I want :)
Trigger a button's onClick from keyinput. or in my case where I want to trigger it from within a function that only runs if the correct keyinputs happen (in OnClick).
I just cannot figure out how I should go about implementing it.. tried to get it to work, no luck (that may have something to do with an all nighter, but who knows) - do you know how to do it? :S
Answer by HTwist · May 19, 2015 at 04:05 AM
If you want to simulate a button click when F and G is pressed, I would do something like that in my MonoBehaviour:
public Button button; //assign the button from the inspector
void Update() {
if(Input.GetKeyDown(KeyCode.F) && Input.GetKeyDown(KeyCode.G)) {
ExecuteEvents.Execute(button.gameObject, new BaseEventData(EventSystem.current), ExecuteEvents.submitHandler);
}
}
It should do it although I haven't tested it.
Answer by sas_88 · May 19, 2015 at 11:01 AM
Hi,
Try with this.
float h;
float v;
bool uimove;
public void MovementHand ()
{
if(!uimove)
{
h=Input.GetAxis ("Horizontal");
v=Input.GetAxis ("Vertical");
}
var move = new Vector3 (h,v, 0);
transform.position += move * speed * Time.deltaTime;
}
void OnGUI()
{
uimove=false;
if (GUI.RepeatButton(new Rect(Screen.width-300, Screen.height-150, 100, 30), "Uparrow"))
{
uimove=true;
h=0;
v=1.0f;
}
if (GUI.RepeatButton(new Rect(Screen.width-300, Screen.height-50, 100, 30), "Downarrow"))
{
uimove=true;
h=0;
v=-1.0f;
}
if (GUI.RepeatButton(new Rect(Screen.width-400, Screen.height-95, 100, 30), "leftarrow"))
{
uimove=true;
h=-1.0f;
v=0;
}
if (GUI.RepeatButton(new Rect(Screen.width-200, Screen.height-95, 100, 30), "Downarrow"))
{
uimove=true;
h=1.0f;
v=0;
}
}
///// press the Ui button make the value as follows
uimove=true;
h=1.0f
v=1.0f
///// Release the Ui button make the value as follows
uimove=false;
Try to implement in this way by assigning value to v&h directly,Input.GetAxis ("Horizontal") & Input.GetAxis ("Vertical") takes maximum value 1 & minimum value -1.