Quantcast
Channel: Questions in topic: "addforce"
Viewing all articles
Browse latest Browse all 1264

How to retrieve the velocity after landing from a jump if the input is correct ?

$
0
0
(It's physics2D) I'm storing the **current velocity** when i jump. When i land it plays an animation, on the first frame of that animation i call an event wich call the function **impulseTimer**. When that function is called it launch a timer. If we pressed the jump button in time i want the player to start running at the same velocity it has right before he jumped. In my code i boosted the value to realy see if it works, but it doesn't. At least not everytime, i don't know why. void Jump() { if(canJump && Input.GetButtonDown("Jump")) { jumpStartX = transform.position.x; velocityAtEnter = rigidbody2D.velocity.x; } if(canJump && Input.GetButton("Jump") && jumpTimer >0) { animator.SetBool("isGrounded", isGrounded); jumpTimer -= Time.deltaTime; rigidbody2D.AddForce(new Vector2(rigidbody2D.velocity.x*3,jumpForce)); } else if(Input.GetButtonUp("Jump") || jumpTimer<0) { canJump = false; } } void FixedUpdate () { //MOVEMENT isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround); animator.SetBool("isGrounded", isGrounded); animator.SetFloat("vSpeed", rigidbody2D.velocity.y); float move = Input.GetAxis("Horizontal"); Jump(); if(isGrounded) { //AFTER A JUMP if(!canJump) { jumpEndX = transform.position.x; jumpTimer = 0.18f; canJump =true; } animator.SetFloat ("speed", Mathf.Abs (rigidbody2D.velocity.x)); rigidbody2D.AddForce(new Vector2(speed*move,0)); if(Mathf.Abs(rigidbody2D.velocity.x) >= maxSpeed) rigidbody2D.velocity = new Vector2(maxSpeed*move, rigidbody2D.velocity.y); if(move >0 && !facingRight) Flip (); else if(move <0 && facingRight) Flip (); } } IEnumerator ImpulseTimer() { float impulseTimer = 0.5f; while(true) { impulseTimer -= Time.deltaTime; if( Input.GetButtonDown("Horizontal") && impulseTimer>0) { float move = Input.GetAxisRaw("Horizontal"); //rigidbody2D.AddForce( new Vector2(speed*move, 0), ForceMode.Impulse); //HERE rigidbody2D.velocity = new Vector2(velocityAtEnter *100, 0); animator.SetFloat ("speed", Mathf.Abs (rigidbody2D.velocity.x)); yield break; } else if (impulseTimer <0) yield break; yield return null; }

Viewing all articles
Browse latest Browse all 1264

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>