Hello. what is the correct condition in which I want the player to pressed a UI button please help! :(
if(Input.GetButtonDown(interactButton)) // this button is from the Input setting where the Interact button is actually a "MOUSE 0" a.k.a Left click and I want this to change to UI button when the player pressed something happens. I dont know how to do that :'( Please help :'(
{
//My statement :)
}
Answer by ArturoSR · Sep 12, 2016 at 01:06 AM
Hello there.
OK, to make the user interact with you app using the new UI system in unity is as follows:
Create a component (script) with a public method/function and attach it to an empty game object or whatever you choose.
Create a UI-Button using the menu item: Game Object > UI > Button.
Select the new button and drag & drop the game object with the component that you create.
Use the drop down menu to select the method/function that you defined within your component, example: public void MakeALotOfNoise() { }, where MakeAlotOfNoise will be the choice.
So, as you can see there's how to make a Button do something inside your app, if you require more info about the new UI, check the documentation, cheers.
Thank you sir for the response I know that thing but my point is How can I changed that key control in which a $$anonymous$$ouse Button to change to a UI button that when pressed the door Opens. and it is in the Update method which attached to my FPS control? It is possible to do that? I dont know how. Newbie here Please help :(
public string interactButton; public Layer$$anonymous$$ask interactLayer; public Image interactIcon; public bool isInteracting;
public float interactDistance = 2f;
void Start()
{
if (interactIcon != null)
{
interactIcon.enabled = false;
}
}
void Update() {
Ray ray = new Ray (transform.position, transform.forward);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, interactDistance , interactLayer))
{
if (isInteracting == false)
{
if(interactIcon != null)
{
interactIcon.enabled = true;
}
if(Input.GetButtonDown(interactButton)) //This is I want to changed to a Button when pressed :( It is possible to make the update public so that we can attached it to a button on click? Please help.
{
if (hit.collider.CompareTag("Door"))
{
hit.collider.transform.parent.GetComponent<Door>().ChangeDoorState();
}
else if(hit.collider.CompareTag("$$anonymous$$ey"))
{
hit.collider.GetComponent<$$anonymous$$ey>().UnlockDoor();
}
}
}
}
else
{
interactIcon.enabled = false;
}
}
If you want to interact directly with your mouse, you need to replace the transform.forward by mouse.position, using the raycast you can control the raycast direction exactly where the mouse cursor point it, again, you can read more about it in the ducumentation, cheers.