UnityScripterAI
Discord ↗
Link Copied to Clipboard
×
"Character controller with hollow knight like movement like wall jumping or dash"
using System.Collections; using System.Collections.Generic; using UnityEngine; public class HollowKnightController : MonoBehaviour { public CharacterController2D controller; public float runSpeed = 40f; public float jumpForce = 400f; public float dashSpeed = 20f; public float wallJumpForce = 300f; public LayerMask wallLayer; private float horizontalMove = 0f; private bool jump = false; private bool dash = false; private bool wallJump = false; private bool isTouchingWall = false; private Vector2 wallJumpDirection; void Update() { horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed; if (Input.GetButtonDown("Jump")) { jump = true; } if (Input.GetButtonDown("Fire1")) { dash = true; } isTouchingWall = Physics2D.OverlapCircle(transform.position, 0.1f, wallLayer); if (isTouchingWall && !controller.m_Grounded && Input.GetButtonDown("Jump")) { wallJump = true; wallJumpDirection = -controller.m_FacingRight ? Vector2.right : Vector2.left; } } void FixedUpdate() { // Move character controller.Move(horizontalMove * Time.fixedDeltaTime, false, jump); jump = false; // Dash if (dash) { Vector2 dashDirection = controller.m_FacingRight ? Vector2.right : Vector2.left; controller.m_Rigidbody2D.AddForce(dashDirection * dashSpeed, ForceMode2D.Impulse); dash = false; } // Wall jump if (wallJump) { controller.m_Rigidbody2D.velocity = new Vector2(0, 0); controller.m_Rigidbody2D.AddForce(new Vector2(wallJumpDirection.x * wallJumpForce, jumpForce), ForceMode2D.Impulse); wallJump = false; } } }