- Home /
Gradual Jump 2d
Hello everyone. I'm trying to make my player jump gradually while the button is pressed, but without success. Could someone give some idea of how to do?
Current code:
if (!jumping && Input.GetMouseButton(0)) {
StartCoroutine (Jump());
}
IEnumerator Jump (){
jumping = true;
float timer = 0.0f;
while (timer <= jumpTime) {
float height = Mathf.Sin(timer / jumpTime * Mathf.PI) * jumpHeight;
transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y+height, 0);
timer += Time.deltaTime;
yield return null;
}
jumping = false;
}
Answer by mealone · Aug 23, 2014 at 05:50 PM
hey, check this out, there is a pretty simple way to make your char jump in this tutorial. http://unity3d.com/learn/tutorials/modules/beginner/2d/2d-controllers
I'm using iTween to move my character and this would not work for me.
Answer by TheDDestroyer12 · Sep 12, 2014 at 05:18 PM
If you wan't it to jump forever, I don't have the answer, but if you want the player to jump and then fall to the ground when the gravity drags it down again, you can use the rigidbody2d.AddForce( /* Vector2 to controll the direction */ )
.
For your purpose, it would look something like this:
rigidbody2d.AddForce(0, jumpPower);
I thought of that but ITween doesn't work very well with rigidbody
Well, why do you use it then? Rigidbodies are like the gravity stuff in unity. If you don't want to use it, I don't know. How do you use gravity then?
$$anonymous$$aybe something with $$anonymous$$arhf.Lerp() ? I'll create something.
Your answer
Follow this Question
Related Questions
Help With Simple Jump Script 1 Answer
GetButtonDown not always firing 1 Answer
Multiple Cars not working 1 Answer
iTween gradual jump 2D 0 Answers