- Home /
How can i use a bool to decide when to move the character or not ?
I got mixed it all and got confused. What i want to do is when i click the right mouse button don't call the GetInteraction(); function when i click on the left mouse button call it. Like a switch/pause button. Click on right will stop calling it click on left will keep calling it. I tried to use the toMove bool variable but messed it all.
private void Update()
{
//if (Input.GetMouseButtonDown(0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
if (Input.GetMouseButtonDown(1) && toMove == true)
{
toMove = false;
}
else
{
if (Input.GetMouseButtonDown(0) && toMove == false && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
{
toMove = true;
GetInteraction();
}
}
}
Answer by RecyclingBen · May 03, 2017 at 11:21 AM
something like this?
void Update() {
if (Input.GetMouseButtonDown(0) && to Move == false &&
!UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject)
{
toMove = true;
}
if (Input.GetMouseButtonDown(1) && toMove == true)
{
toMove = false;
}
if (toMove == true)
{
GetInteraction();
}
}
I'm not actually sure what GetInteraction() does or is supposed to do, but hopefully it's something like this that you are looking for? If this doesn't work could you provide if GetInteraction();
is something you are trying to call every frame when`toMove = true` or something you only want to call when it's true and you are right-clicking.
Anyways, hope this works
Answer by antx · May 03, 2017 at 11:42 AM
I'm not sure how exatly you want this to work. Should GetInteraction() only be called while the left button is pressed? Or do you want GetInteraction() to be continuously called after a left click until you click right?
This will call GetInteraction() as long you hold left and the cursor is not over an object:
void Update()
{
if (Input.GetMouseButtonDown(0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
{
GetInteraction();
}
}
This will initiate a constant calling of GetInteraction() with a left click until you right click to cancel:
private bool moving = false;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
moving = true;
}
if (Input.GetMouseButtonDown(1))
{
moving = false;
}
if (moving && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
{
GetInteraction();
}
}
I could think of some more cases but maybe those 2 are already helpful?
Your answer
Follow this Question
Related Questions
How can i prevent from mouse to collide with thirdpersoncontroller ? 0 Answers
How can i lock the mouse cursor in the middle of the screen ? 1 Answer
Why InputField don't have the property text ? 2 Answers
How can i change the walls height and keep the size for example 100x100 but height 0.1 ? 1 Answer
How can i using a break point if a gameobject have a collider after added to it ? 1 Answer