- Home /
Increase Jump Through Mouse Clicks?
A quick beginner question(I know shameful). How can I increase the jump force of my Player depending on how many mouse clicks i do? For an example If I click the mouse 1 time, it would increase my Jump force by 10. If i click it 3 times it would increase it by 30 since 10 x 3 = 30. If I click it 100 times it would increase it by 1000, and so on. So how can this be done? I know a simple mouse click if statement, but not by how many times the mouse is clicked. That's the hard part. Here's my Player script, very simple:
public class TestCharacterController : MonoBehaviour {
public float maxSpeed = 10f;
bool facingRight = true;
public float jumpForce = 1f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
float move = Input.GetAxis ("Horizontal");
rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
if (move > 0 && !facingRight)
Flip ();
else if (move < 0 && facingRight)
Flip ();
}
void Update(){
if(Input.GetMouseButton(0))
rigidbody2D.AddForce(new Vector2(0, jumpForce));
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
I NEED it to keep track of how many times the mouse button is click. Then it could add it up and apply it when I want it to(things just got complicated, which is fun!). So to sum it up, clicks mouse button how every many times, keep track of how many mouse clicks and adds the proper jump force, it stores it away until I want to apply it at any time(whether by coroutines or something else). Simple? Maybe but a little too complex for me. Please help if you can.
Answer by Noob_Vulcan · Aug 28, 2014 at 11:21 AM
Simply take a variable say
int clickCount; // Can be static if you want
if(Input.GetMouseButton(0)){
clickCount++;
//can put conditions based number of clicks . eg
if(clickCount==5)
// Add this much force and after this you want to reset the clicks just set clickCount==0
rigidbody2D.AddForce(new Vector2(0, jumpForce));
}
I hope this is what you want.
I'm starting to get what i want, but i'm having a problem. The number of mouse clicks don't work anymore. So now I only have to press it once in order to jump, which is not what I want. I use a coroutine for reasons that are not too important, but anyways here's the code:
// Use this for initialization
void Start () {
mikki = false;
StartCoroutine (Clicks());
}
IEnumerator Clicks()
{
while(true){
yield return new WaitForSeconds (0.001f);
if (Input.Get$$anonymous$$ouseButton (0)) {
clickCount++;
if (clickCount == 5){
mikki = true;
Debug.Log("WOR$$anonymous$$ YOU BB");
}
}
}
}
How can I fix the number of mouse clicks?
@Dogg : your input code should be in Update. the way you used may fail in some cases.
clickCount is not increasing when you click ?????
Yes, it would appear so. The click count is no longer working now that I moved it out of Update and into the Ienumerator, but that shouldn't matter. I only want the click count to be once and after a short amount of time disable me being able to click the mouse again. Only once on start, and not every frame or whatever Update does again. What I'm trying to do is, click mouse 5 times, increase jump, disable me being able to click the mouse again, and loop the jump so it looks like I'm flying. That's why on the code you see while(true), because that seems to loop the jump and get what I want, but again the mouse clicks aren't working properly.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
please i beg of you help!!!!!!!!!!!!!!! 2 Answers
How to make Sphere to jump? 2 Answers
Switching Cameras at runtime 1 Answer