We can control:
- The Highest Jump Height (JumpVelocity and Gravity)
- The velocity he goes Up (JumpVelocity and Gravity)
- The velocity he goes Down (Fall Multiplier)
- The Lowest Jump Height (Low Jump Multiplier)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class Jump : MonoBehaviour { Rigidbody2D rb; public float speed = 5; // Horizontal speed to move public float gravity = -5; // force that influences when jumping (going up with button, going up without button, falling) // this force stops the jump for going up. public float jumpVelocity; // velocity set for the jump public float fallMultiplier = 2.5f; // force that inflences when the player is falling public float lowJumpMultiplier = 2f; // force that influences when the player is going up withot pressing the button float hor; public LayerMask whatIsGround; // what is floor? public Transform feetPos; // where the isGrouded OverlapCircle is positioned public float checkRadius; // the radious of OverlapCircle bool isGrounded; // if its grounded or not private void Start() { rb = GetComponent<Rigidbody2D>(); } private void Update() { hor = Input.GetAxis("Horizontal"); isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround); if (Input.GetButtonDown("Jump") && isGrounded) { GetComponent<Rigidbody2D>().velocity = Vector2.up * jumpVelocity; } } private void FixedUpdate() { rb.velocity = new Vector2(hor * speed, rb.velocity.y); if (rb.velocity.y < 0) // if is falling, accelerate { rb.velocity += Vector2.up * (-fallMultiplier); } else if (rb.velocity.y > 0 && !Input.GetButton("Jump")) // if is going up and not pressing button, force down { rb.velocity += Vector2.up * (-lowJumpMultiplier); } // always applied if not grounded if (!isGrounded) rb.velocity += Vector2.up * (gravity); } private void OnDrawGizmos() { bool isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround); if (isGrounded) { Gizmos.color = Color.red; Gizmos.DrawWireSphere(feetPos.position, checkRadius); } else { Gizmos.color = Color.green; Gizmos.DrawWireSphere(feetPos.position, checkRadius); } } } |
Nenhum comentário:
Postar um comentário