How do I get perform a single button press to call a method? - unity3d

I would like a Unity progress slider to fill up when I press the spacebar. Currently, I have to mash the spacebar 100 times to fill up the progress bar.
It fills the progress slider automatically when moved to update().
It fills the progress slider when I hold the spacebar if I change Input.GetKeyDown to Input.GetKey.
But I don't want to hold the spacebar down. I want to press it once to start have the slider value gradually fill up the progress bar.
void Start()
{
sliderValue = 1.0f;
}
void Update()
{
pressSpacebar();
}
void pressSpacebar()
{
if (Input.GetKeyDown("space"))
{
filluptheslider();
}
}
void filluptheslider()
{
ProgressSlider.value += sliderValue * Time.deltaTime;
}

There are a few ways to handle this:
Method 1
Boolean value in the class to track the pressed state:
bool pressed = false;
void pressSpacebar()
{
if (Input.GetKeyDown("space"))
{
filluptheslider();
}
if(pressed)
{
filluptheslider();
}
}
Method 2
Coroutine that starts after space is pressed.
void pressSpacebar()
{
if (Input.GetKeyDown("space"))
{
StartCoroutine(FillUpTheSlider);
}
}
IEnumerator FillUpTheSlider()
{
while(ProgressSlider.value < ProgressSlider.maxValue)
{
ProgressSlider.value += sliderValue * Time.deltaTime;
yield return null;
}
}
If you press the space bar multiple times, multiple coroutines will spawn and it will fill faster. You can optionally add the following before you start the coroutine to prevent this behaviour:
StopCoroutine(FillUpTheSlider)
You could alternatively combine method 1 and 2 to remove this behaviour.

bool isFull = false;
bool isPressed = false;
void Update
{
If (Input.GetKeyDown(KeyCode.Space)
&&
!isFull)
{
isPressed = true;
}
if(isPressed)
{
//if you want fill to increase every 0.05 seconds.
StartCoroutine(FillGradually(0.05f));
isPressed = false;
}
}
Ienumerator FillGradually(float fillOffset)
{
//If 100 is your max value
while(ProgressSlider.value < 100)
{
ProgressSlider.value += 1 * Time.deltaTime;
yield return new WaitForSeconds(fillOffset);
}
isFull = true;
}

You can just use a bool condition and when space is pressed, you can just make it true once and when it is true, it will automatically start filling.
bool isSpacePressed; //bydefault, bool class variable is set to false //when initialized
update()
{
if(isSpacePressed==true) //no need to mention ==true just added to make it //readable
{
image.fillAmount=fillvalue+0.1f;
}
}
if(input.GetKey(keyCode.Space)==true)
{
isSpacePressed=true;
}

Related

How can I connect button to function?

we have a dash button in the game. If we press the button dash button is working well but the problem is if we press anywhere on screen it is working too! We only want to work that function with button.
Anyone help us please?
Code:
public void Dash()
{
if (Input.GetMouseButtonDown(0))
{
activeMoveSpeed = dashSpeed;
dashCounter = dashLength;
}
if (dashCounter > 0)
{
canShoot = false;
dashCounter -= Time.deltaTime;
if (dashCounter <= 0)
{
canShoot = false;
activeMoveSpeed = speed;
dashCoolCounter = dashCoolDown;
}
}
if (dashCoolCounter > 0)
{
canShoot = false;
dashCoolCounter -= Time.deltaTime;
}
}
private void FixedUpdate()
{
Movement();
Dash();
}
Button and Game
Button
The function you specify in OnClick will be called whenever the button is clicked.
Currently, you are not using the button at all. I think you meant something like this (StartDash() is the function that should be chosen in OnClick):
public void StartDash()
{
activeMoveSpeed = dashSpeed;
dashCounter = dashLength;
}
private void DoDash()
{
if (dashCounter > 0)
{
canShoot = false;
dashCounter -= Time.deltaTime;
if (dashCounter <= 0)
{
canShoot = false;
activeMoveSpeed = speed;
dashCoolCounter = dashCoolDown;
}
}
if (dashCoolCounter > 0)
{
canShoot = false;
dashCoolCounter -= Time.deltaTime;
}
}
private void FixedUpdate()
{
Movement();
DoDash();
}
Take a look at the related pages in the Unity Documentation:
Manual - Button
Scripting - UI.Button
Scripting - UI.Button.onclick

The punching bool is not setting the animation variable to true to false

In a nutshell, I want to code a punching script where every time the player presses a j key, the punching animation gets played. The issue is that the punching animation is not playing. Here is my code along with my screenshots with the Animator, and my animation for the punching animation.
void Start()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.J)) // attack
{
print("Times they have punched");
punching = true;
ispunching(punching);
} else
{
punching = false;
ispunching(punching);
}
}
void ispunching(bool punch)
{
if(punch)
{
print("Punching: " + punch);
anim.SetBool("Punch", punch);
waittilpunchanimfinish(1f);
}
else if (!punch)
{
print("Not punching" + punch);
anim.SetBool("Punch", punch);
}
}
IEnumerator waittilpunchanimfinish(float time)
{
yield return new WaitForSeconds(time);
punching = false;
}
Here is the image right here. I know the idle animation is not transitioned to the punching. I did it on purpose to simplify things so people can fix it easier :
The Punching Animation Picture Keyframe
In Update you do
if(Input.GetKeyDown(KeyCode.J)) // attack
{
print("Times they have punched");
punching = true;
ispunching(punching);
} else
{
punching = false;
ispunching(punching);
}
and thereby immediately reset the punching to false in the next frame where GetKeyDown will not be true anymore!
I would instead rather do the entire punch routine inside of the Coroutine like
// The only purpose left for this flag is for not having multiple
// Corouintes at the same time.
private bool punching;
private void Start()
{
anim = GetComponent<Animator>();
}
private void Update()
{
// Only receive input if not already punching
if(!punching && Input.GetKeyDown(KeyCode.J)) // attack
{
StartCoroutine(Punch());
}
}
// Handle the entire punch routine in one go
private IEnumerator Punch()
{
print("Start punching");
punching = true;
anim.SetBool("Punch", true);
yield return new WaitForSeconds(1f);
anim.SetBool("Punch", false);
punching = false;
print("Finished punching");
}

Unity Crashes when Using this UI

using UnityEngine.UI;
using UnityEngine;
public class Tutorial : MonoBehaviour
{
public Text tutorialText;
void Update()
{
tutorialText.text = "Press [SPACE] to Jump... (obviously...)";
if (Input.GetKeyDown(KeyCode.Space))
{
while (true)
{
tutorialText.text = "Now Shoot";
if (Input.GetKeyDown(KeyCode.Mouse0))
{
while (true)
{
tutorialText.text = "Nice";
}
}
}
}
}
}
Change your code to that, because right now you are in a infinite loop which sets your tutorialtext to "Now Shoot" again and again until Unity crashes.
if (Input.GetKeyDown(KeyCode.Space))
{
tutorialText.text = "Now Shoot";
}
else if (Input.GetKeyDown(KeyCode.Mouse0))
{
tutorialText.text = "Nice";
}
If you want the text to disappear after the first time you done the actions you should add bool variables.
bool jumped = false;
bool hasShot = false;
And then implement thoose bools into your code like this:
if (Input.GetKeyDown(KeyCode.Space))
{
if (jumped)
return;
tutorialText.text = "Now Shoot";
jumped = true;
}
else if (Input.GetKeyDown(KeyCode.Mouse0))
{
if (hasShot || !jumped)
return;
tutorialText.text = "Nice";
hasShot = true;
}
You also need to move the first text definition out of Update, because Update is called each frame. Put it into void Start() instead
void Start() {
tutorialText.text = "Press [SPACE] to Jump... (obviously...)";
}

How to change the size of circlecollider2d for X amount of time

I would like to use my circlecollider2d as a sort of force field to push objects back for a given amount of time, on button press. Here is the code I have, to simply turn it on and off again.
void Update()
{
if (Input.GetKey(KeyCode.Space))
{
GetComponent<CircleCollider2D>().radius = 25.0f;
}
else
{
GetComponent<CircleCollider2D>().radius = 2.1f;
}
}
You can use Invoke to call a function after x number of seconds. I modified your code below to clear the force field 5 seconds after it is activated. The forceFieldActive flag will prevent it from being activated again while it's already active.
int forceFieldDuration = 5; //5 second duration
bool forceFieldActive = false;
void Update()
{
if (Input.GetKey(KeyCode.Space) && forceFieldActive == false)
{
forceFieldActive = true;
GetComponent<CircleCollider2D>().radius = 25.0f;
Invoke("DisableForceField", 5);
}
}
void DisableForceField()
{
GetComponent<CircleCollider2D>().radius = 2.1f;
forceFieldActive = false;
}
Note: You can also use a coroutine which is more efficient as Draco18s mentioned in the comments. It would look like this:
int forceFieldDuration = 5; //5 second duration
bool forceFieldActive = false;
void Update()
{
if (Input.GetKey(KeyCode.Space) && forceFieldActive == false)
{
forceFieldActive = true;
GetComponent<CircleCollider2D>().radius = 25.0f;
StartCoroutine(DisableForceField());
}
}
IEnumerator DisableForceField()
{
yield return new WaitForSeconds(forceFieldDuration);
GetComponent<CircleCollider2D>().radius = 2.1f;
forceFieldActive = false;
}

Unity3D for Google Cardboard

I have a VR Scene with a C# Script on the camera that allows the user to Click Once to move and again to stop.
public float speed = 1.0f;
public bool startedMoving = true;
public GameObject myCam;
// Update is called once per frame
void Update () {
if (startedMoving) {
transform.position += myCam.transform.forward * speed * Time.deltaTime;
}
// if(Input.GetButtonDown("Fire1")){
if (Input.GetMouseButton(0)){
startedMoving = !startedMoving;
}
}
What I want to know is how I can CLICK & HOLD to move Backwards..?
Thank you!
~ b
Use enum the represent the status of the mouse instead of startedMoving or multiple booleans that will make everything easier to implement. The comment in the code describes how it works.
using UnityEngine;
using System.Collections;
public class ClickAndHeld : MonoBehaviour
{
public GameObject myCam;
CLICK_MODE clickMode = CLICK_MODE.NO_CLICK;
MOVE_DIRECTION moveDir = MOVE_DIRECTION.IDLE;
public float speed = 1.0f;
//If down for 0.5 secods the it is considered Click and Hold instead of Click
float clickHoldDetectTime = 0.5f;
float clickCounter = 0; //Dont not change
void Start()
{
StartCoroutine(mover());
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0))
{
//If in NO_CLICK, set to CLICKED
if (clickMode == CLICK_MODE.NO_CLICK)
{
//Change the mode to CLICKED
clickMode = CLICK_MODE.CLICKED;
}
//If in CLICKED mode, start counting to clickHoldDetectTime
if (clickMode == CLICK_MODE.CLICKED)
{
clickCounter += Time.deltaTime; //Increment counter
//Check if we have reached the clickHoldDetectTime time
if (clickCounter > clickHoldDetectTime)
{
//Reset Counter
clickCounter = 0;
//Change the mode to CLICK_AND_HELD
clickMode = CLICK_MODE.CLICK_AND_HELD;
}
}
//If STILL down and the the current mode is CLICK_AND_HELD then do clickedAndHeldDown stuff
if (clickMode == CLICK_MODE.CLICK_AND_HELD)
{
clickedAndHeldDown();
}
}
else
{
//If released and the current mode is CLICKED then do clicked stuff
if (clickMode == CLICK_MODE.CLICKED)
{
clicked();
}
//If released and the current mode is CLICK_AND_HELD, change to RELEASED then do relased stuff
if (clickMode == CLICK_MODE.CLICK_AND_HELD)
{
//Change the mode to RELEASED
clickMode = CLICK_MODE.RELEASED;
mouseReleasedAfterBeingHeld();
}
//Reset each time mouse button is released
reset();
}
}
IEnumerator mover()
{
while (true)
{
if (moveDir == MOVE_DIRECTION.IDLE)
{
}
//Move Forward
if (moveDir == MOVE_DIRECTION.FORWARD)
{
transform.position += myCam.transform.forward * speed * Time.deltaTime;
}
//Move Backward
if (moveDir == MOVE_DIRECTION.BACKWARD)
{
transform.position -= myCam.transform.forward * speed * Time.deltaTime;
}
yield return null;
}
}
private void clicked()
{
Debug.Log("CLICKED");
//If Idle, become Forward
if (moveDir == MOVE_DIRECTION.IDLE)
{
moveDir = MOVE_DIRECTION.FORWARD;
}
//If forward, moves become idle
else if (moveDir == MOVE_DIRECTION.FORWARD)
{
moveDir = MOVE_DIRECTION.IDLE;
}
//--------------------------------------------------
//If backward, moves become idle
else if (moveDir == MOVE_DIRECTION.BACKWARD)
{
moveDir = MOVE_DIRECTION.IDLE;
}
}
private void clickedAndHeldDown()
{
Debug.Log("CLICKED AND HELD");
//If Idle, becomes backward
if (moveDir == MOVE_DIRECTION.IDLE)
{
moveDir = MOVE_DIRECTION.BACKWARD;
}
}
//Called when released after being RELEASED from CLICKED_HELD
private void mouseReleasedAfterBeingHeld()
{
Debug.Log("RELEASED AFTER CLICKED AND HELD");
//If backward, move becomes idle
if (moveDir == MOVE_DIRECTION.BACKWARD)
{
moveDir = MOVE_DIRECTION.IDLE;
}
//--------------------------------------------------
//If forward, move becomes idle
else if (moveDir == MOVE_DIRECTION.FORWARD)
{
moveDir = MOVE_DIRECTION.IDLE;
}
}
void reset()
{
clickMode = CLICK_MODE.NO_CLICK;
clickCounter = 0;
}
}
public enum CLICK_MODE
{
NO_CLICK, CLICKED, CLICK_AND_HELD, RELEASED
}
public enum MOVE_DIRECTION
{
IDLE, FORWARD, BACKWARD
}
Because you only have one trigger action you're going to have to implement something time based for toggling forward/backwards movement. For example, if you press and release the trigger quickly then forward walk could be toggled, but if you're holding the trigger after N length of time then walk backwards.
He's a practical example to get you starting.
Pressing and releasing the trigger within 300ms will toggle forward movement
Pressing and holding the trigger for longer than 300ms will begin backwards movement, releasing the trigger then will stop backwards movement
This is a theoretical example
public float speed = 1.0f;
bool triggerPressed = false;
float triggerHeldTime = 0f;
public bool movingForwards = false;
public bool movingBackwards = false;
void Update ()
{
// increment hold time if we're still holding trigger
if (Input.GetMouseButton(0) && triggerPressed)
triggerHeldTime += Time.deltaTime;
if (Input.GetMouseButton(0) && !triggerPressed)
{
// reset everything when trigger initially pressed
movingForards = false;
movingBackwards = false;
triggerHeldTime = 0f;
triggerPressed = true;
}
else if (!Input.GetMouseButton(0) && triggerPressed)
{
// upon trigger release
triggerPressed = false;
// if we are not moving backwards, toggle forwards movement
if(!movingBackwards)
movingForwards = !movingForwards;
// always reset backwards movement when we release the trigger
movingBackwards = false;
triggerHeldTime = 0f;
}
// if the trigger has been held for 300ms then move backwards
if(triggerHeldTime > 0.3f)
{
moveForwards = false;
moveBackwards = true;
}
// actually perform the movement
if (moveForwards)
{
transform.position += myCam.transform.forward * speed * Time.deltaTime;
}
else if(moveBackwards)
{
transform.position -= myCam.transform.forward * speed * Time.deltaTime;
}
}