ESP32 Ventilation Diagnostic Module Design
1:27
Clarifying scripting response.
Navigating tool usage for Unity.
Considering jump mechanics.
Building a PlayerController2D script.
Creating a PlayerController2D script.
Csharp
using UnityEngine; public class PlayerController2D : MonoBehaviour { [Header("Movement")] public float moveSpeed = 8f; [Header("Jumping")] public float jumpForce = 14f; public int extraJumps = 1; // double-jump => 1 public float coyoteTime = 0.10f; // grace after leaving ground public float jumpBuffer = 0.10f; // buffer before landing [Header("Walls")] public float wallSlideSpeed = 2f; public Vector2 wallJumpForce = new Vector2(12f, 14f); public float wallJumpControlLock = 0.18f; // brief horizontal lock after wall-jump [Header("Checks")] public Transform groundCheck; public Transform wallCheckLeft; public Transform wallCheckRight; public float checkRadius = 0.18f; public LayerMask groundLayer; Rigidbody2D rb; int jumpsLeft; float coyoteCounter; float jumpBufferCounter; float lastWallJumpTime; bool grounded, onWallLeft, onWallRight; float inputX; bool facingRight = true; void Awake() { rb = GetComponent(); jumpsLeft = extraJumps; } void Update() { // Input inputX = Input.GetAxisRaw("Horizontal"); if (Input.GetButtonDown("Jump")) jumpBufferCounter = jumpBuffer; else jumpBufferCounter -= Time.deltaTime; // Checks grounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, groundLayer); onWallLeft = Physics2D.OverlapCircle(wallCheckLeft.position, checkRadius, groundLayer); onWallRight = Physics2D.OverlapCircle(wallCheckRight.position, checkRadius, groundLayer); bool onWall = (onWallLeft || onWallRight) && !grounded; int wallDirX = onWallLeft ? -1 : (onWallRight ? 1 : 0); // Coyote + jump reset if (grounded) { coyoteCounter = coyoteTime; jumpsLeft = extraJumps; } else coyoteCounter -= Time.deltaTime; // Wall slide if (onWall && rb.velocity.y 0f) { if (grounded || coyoteCounter > 0f) { Jump(Vector2.up * jumpForce); } else if (onWall) { // Push off opposite to wall Vector2 v = new Vector2(-wallDirX * wallJumpForce.x, wallJumpForce.y); Jump(v); lastWallJumpTime = Time.time; } else if (jumpsLeft > 0) { Jump(Vector2.up * jumpForce); jumpsLeft--; } } // Horizontal move (briefly limited after wall-jump) bool lockControl = Time.time 0 && !facingRight) || (h < 0 && facingRight)) Flip(); } } void Jump(Vector2 impulse) { // consume buffer now jumpBufferCounter = 0f; // keep existing x, replace y (for consistent height) rb.velocity = new Vector2(rb.velocity.x, 0f); rb.velocity += impulse; } void Flip() { facingRight = !facingRight; var s = transform.localScale; s.x = Mathf.Abs(s.x) * (facingRight ? 1f : -1f); transform.localScale = s; } void OnDrawGizmosSelected() { if (groundCheck) { Gizmos.color = Color.green; Gizmos.DrawWireSphere(groundCheck.position, checkRadius); } if (wallCheckLeft) { Gizmos.color = Color.cyan; Gizmos.DrawWireSphere(wallCheckLeft.position, checkRadius); } if (wallCheckRight) { Gizmos.color = Color.cyan; Gizmos.DrawWireSphere(wallCheckRight.position, checkRadius); } } }
Input.GetKeyDown(KeyCode.Space) if preferred.extraJumps for triple jumps.wallJumpControlLock for more/less “push-off” feel.coyoteTime and jumpBuffer for forgiveness.Ready.
Reviews
Properties
Pricing & Availability
Distributor | Qty 1 |
|---|
Assets
portal-symbol.svg
portal-symbolDefaultControls