how do i make a button do something in unity when a player goes up to it and press e on it. lets say it should enable and explosion on the level, how do you do that?
Here is a YouTube link that will explain a thing or two about Unity’s new UI and the buttons of it :
UI Button - Unity Official tutorials - YouTube ;
I could type the process of what you need to do to achieve the result you want but i’m thinking it would be easier for you if you could watch some one do it.
the answer you are looking for is at the 8 minutes or so but it wouldn’t hurt to watch the full 10 minutes of it.
Good luck
here is the code I used along with some comments to help you understand what the code does :
using UnityEngine; using System.Collections;
public class RaycastScript : MonoBehaviour{
Ray myRay; // initializing the ray
RaycastHit hit; // initializing the raycasthit
void Update ()
{
myRay = Camera.main.ScreenPointToRay (Input.mousePosition); // telling my ray variable that the ray will go from the center of
// my main camera to my mouse (This will give me a direction)
if (Physics.Raycast (myRay, out hit)) { // here I ask : if myRay hits something, store all the info you can find in the raycasthit varible.
// things like the position where the hit happend, the name of the object that got hit etc…etc…
if (Input.GetMouseButtonDown (0)) {// what to do if i press the left mouse button
if(hit.tag =="NextLevel"){
application.loadLevel(“Level2”);
} // instatiate a prefab on the position where the ray hits the floor.
Debug.Log (hit.point);// debugs the vector3 of the position where I clicked
}// end upMousebutton
}// end physics.raycast
}// end Update method
}// end class