What I wnat to happen is when the user presses this button, it remains down (active), so when the player then clicks on the screen a waypoint point appears, then once finished the player should click ‘Create waypoint’ again to deactive the waypoint placement system.
How would one go about doing this?
presumably I also need to stick something in the update function as well to make the instantiated object appear.
void OnGUI() {
if (GUI.RepeatButton(new Rect (0,0, 500, 100), "Create Waypoint")){
float mousex = Input.mousePosition.x;
float mousey = Input.mousePosition.y;
Ray ray = camera.ScreenPointToRay (new Vector3(mousex,mousey,0));
RaycastHit hit;
if (Input.GetMouseButtonDown(0)){
if (Physics.Raycast(ray, out hit))
{
Transform waypoints = Instantiate(waypointObject, hit.point, Quaternion.identity) as Transform ;
}
}
}
}
var a =0;
function OnGUI()
{
if (GUI.Button(new Rect (0,0, 500, 100), "Create Waypoint"))
{
if(a == 0)
{
a=1;
}
else
{
a=0;
}
print(a);
}
if (Input.GetMouseButtonDown(0) a==1)
{
// if a is set to one then your code will work inside this if statement
// and i am not sure abt the Raycast inside OnGUI, never tried that
}
}
The first section of your code works, its printing a in the console.
However its not taking the Input Button down If statement.
my current code:-
int a = 0;
void OnGUI ()
{
if (GUI.RepeatButton (new Rect (0, 400, 500, 100), "Create Waypoint")) {
if (a == 0) {
a = 1;
} else {
a = 0;
print (a); // prints 'a' in the console.
if (Input.GetMouseButtonDown (1) a == 1) { // This doesn't appear to be doing anything.
Debug.Log ("waypointactive");
}
}
}
}
Also having just put my raycast code stright in on the button press in the OnGUI it does seem to work, yay!
c#, you’ll have to convert to javascript.
The button changes to red when on as well.
It’s a GUILayout. Obviously can be easily changed to a normal GUI element.
Bronco78th u jst messed up all the braces, so jst care fully watch what i have done, it will work i tried it my self and it worked for me.
for Raycast do something like this Because i am not sure that raycast will will inside OnGUI or not.
var a1 =0;
var raycastActive = 0;
function Update()
{
var mousex : float = Input.mousePosition.x;
var mousey : float = Input.mousePosition.y;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if(raycastActive == 1)
{
if (Input.GetMouseButtonDown(0) )
{
if (Physics.Raycast (ray, hit, 100))
{
var pos : Vector3 = hit.point;
print(pos);
}
}
}
}
function OnGUI()
{
if (GUI.Button(new Rect (0,0, 500, 100), "Create Waypoint"))
{
if(a1 == 0)
{
a1=1;
}
else
{
a1=0;
}
print(a1);
}
if(a1 == 1)
{
raycastActive = 1;
}
else if(a1==0)
{
raycastActive = 0;
}
}
this is tested JS code and i have no errors. And dont use Repeat Button, this can be done with GUI.Button