Question by
Sad_Man30 · Aug 28, 2020 at 09:29 PM ·
2d gameandroid buildrpg-game
is there any tutorial for ui button for character to interact with npc or object?
im making a android 2d rpg. i cant seem to call the button as a key all the tutorial i see all use Input.GetKeyDown(KeyCode.Space) i want to use the button to call it, are their any tutorial for these?
Comment
DId you ever figure this out? ive been having the same problem. Ive been searching for 3/4 days now.
What do you mean by button? Input.GetButton("Fire1");
or UI Button?
Its, just, that i couldnt understand the issue. So you want to simulate a keypress with an UI Button, right?
Answer by Hellium · Sep 07, 2020 at 07:18 AM
PSEUDO-CODE (not actual code)
// To be attached to player
Interactable interactable; // Script to attach to other object
OnTriggerEnter(Collider other)
Interactable i = other.GetComponent<Interactable>();
if( i != null )
interactable = i:
OnTriggerExit(Collider other)
Interactable i = other.GetComponent<Interactable>();
if( i != null && interactable == i)
interactable = null:
public void Interact() // to be called by button
if(interactable != null)
interactable.Interact();
public interface Interactable : MonoBevahiour
{
void Interact();
}
public class YourObject : MonoBevahiour, Interactable
{
public void Interact()
{
Debug.Log("Someone interacted with me");
}
}
public class YourOtherObject : MonoBevahiour, Interactable
{
public void Interact()
{
Debug.Log("Hello world");
}
}