- Home /
Sequence of function Calls
Hi,
I am trying to get a cube to display some text if the player rightclicks on it and is close enough to it. If the player is not close enough, he will automatically walk towards it and display the text as soon as he is close enough. If the player chooses to walk somewhere else instead, the function should stop.
I thought I would have a a bool that gets set to false when the player clicks on the cube and gets set to true if he clicks somewhere else and the message would only display when that bool is set to false. So far so good. However, everytime I click on the cube, it sets the bool to false and then immediatly to true again.
So my question would be if I can somehow determine in which order those commands are being executed?
Also, I am completely new to this (literally started yesterday), so if there is a much better way to achieve this, please enlighten me :D
Here`s the code I have so far (the walking is handled in a different script that is working fine):
#pragma strict
var CubeObject :GameObject;
var Player :GameObject;
private var distance :float;
private var RMBclicked :boolean;
function OnMouseOver()
{
if (Input.GetMouseButtonDown(1))
{
RMBclicked = false;
print ("false");
}
}
function Update ()
{
distance = Vector3.Distance(CubeObject.transform.position, Player.transform.position);
if (Input.GetMouseButtonDown (1))
{
RMBclicked = true;
print ("true");
}
if (distance <3)
{
if (RMBclicked == false)
{
print ("Hello");
//RMBclicked = true;
//print ("true Dist");
}
}
}
function LateUpdate ()
{
}
Answer by cmpgk1024 · Nov 09, 2013 at 08:03 PM
You are setting RMBClicked to both true and false every time you click the right mouse button. Try using this:
function Update ()
{
distance = Vector3.Distance(CubeObject.transform.position, Player.transform.position);
if (Input.GetMouseButtonDown (1) && distance < 3)
{
print ("hello");
}
else if(distance > 3){
RMBclicked = true;
}
if(RMBclicked){
//move player to cube
RMBclicked = false;
}
}
Answer by Fraeger · Nov 09, 2013 at 10:31 PM
Hello! Thank you very much for the help, however there is still a little problem. when the player is too far from the cube, it will set the button to true and the player will walk there. But it will not check for distance and RMBclicked again, so it will not display the message.
However, I did fix it with the help of a friend by using getMouseButtonUp instead of GetMouseButtonDown (thereby creating a small delay, setting the bool first to false and then to true).
So this it what it looks like in case anybody is interested.
var CubeObject :GameObject; var Player :GameObject; private var distance :float; private var RMBclicked :boolean;
function OnMouseOver() {
if (Input.GetMouseButtonUp(1))
{
RMBclicked = false;
print ("false");
}
}
function Update () { distance = Vector3.Distance(CubeObject.transform.position, Player.transform.position);
if (Input.GetMouseButtonDown (1))
{
print ("true");
RMBclicked = true;
}
if (distance <3)
{
if (RMBclicked == false)
{
print ("Hello");
//RMBclicked = true;
//print ("true Dist");
RMBclicked = true;
}
}
}