Hi I’m new to Unity and I’m having trouble with coding a walljump. For some reason, when I press the jump button, sometimes the walljump just doesn’t work. The x-movement works fine but the y-movement doesn’t work. It works sometimes but other times only the x works and i still fall downwards. I changed the gravity a few times but I fell at the same rate. I also changed the y-Walljump force but it was the same. PS: You only need to really look at the lines of code all the way to “private void awake”.
My Code:
using UnityEngine;
using UnityEngine.Events;
public class CharacterController2D : MonoBehaviour
{
[SerializeField] private float m_JumpForce = 400f;
[Range(0, 1)] [SerializeField] private float m_CrouchSpeed = .36f;
[Range(0, .3f)] [SerializeField] private float m_MovementSmoothing = .05f;
[SerializeField] private bool m_AirControl = false;
[SerializeField] private LayerMask m_WhatIsGround;
[SerializeField] private Transform m_GroundCheck;
[SerializeField] private Transform m_CeilingCheck;
[SerializeField] private Collider2D m_CrouchDisableCollider;
const float k_GroundedRadius = .2f;
private bool m_Grounded;
const float k_CeilingRadius = .2f;
const float k_FrontRadius = .2f;
private Rigidbody2D m_Rigidbody2D;
private bool m_FacingRight = true;
private Vector3 m_Velocity = Vector3.zero;
bool isTouchingFront;
public Transform FrontCheck;
bool wallSliding;
public float wallSlidingSpeed;
bool wallJumping;
public float xWallForce;
public float yWallForce;
public float wallJumpTime;
[Header("Events")]
[Space]
public UnityEvent OnLandEvent;
[System.Serializable]
public class BoolEvent : UnityEvent<bool> { }
public BoolEvent OnCrouchEvent;
private bool m_wasCrouching = false;
private void Update()
{
isTouchingFront = Physics2D.OverlapCircle(FrontCheck.position, k_FrontRadius, m_WhatIsGround);
if (isTouchingFront == true && m_Grounded == false)
{
wallSliding = true;
}
else
{
wallSliding = false;
}
if (wallSliding)
{
m_Rigidbody2D.velocity = new Vector2(m_Rigidbody2D.velocity.x, Mathf.Clamp(m_Rigidbody2D.velocity.y, -wallSlidingSpeed, Input.GetAxisRaw("Horizontal") * 4));
}
if (Input.GetButtonDown("Jump") && wallSliding == true)
{
wallJumping = true;
}
else
{
wallJumping = false;
}
if (wallJumping)
{
m_Rigidbody2D.velocity = new Vector2(xWallForce * -Input.GetAxisRaw("Horizontal"), yWallForce);
}
}
private void Awake()
{
m_Rigidbody2D = GetComponent<Rigidbody2D>();
if (OnLandEvent == null)
OnLandEvent = new UnityEvent();
if (OnCrouchEvent == null)
OnCrouchEvent = new BoolEvent();
}
private void FixedUpdate()
{
bool wasGrounded = m_Grounded;
m_Grounded = false;
Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders*.gameObject != gameObject)*
-
{*
-
m_Grounded = true;*
-
if (!wasGrounded)*
-
OnLandEvent.Invoke();*
-
}*
-
}*
-
}*
-
public void Move(float move, bool crouch, bool jump)*
-
{*
-
if (!crouch)*
-
{*
-
if (Physics2D.OverlapCircle(m_CeilingCheck.position, k_CeilingRadius, m_WhatIsGround))*
-
{*
-
crouch = true;*
-
}*
-
}*
-
if (m_Grounded || m_AirControl)*
-
{*
-
if (crouch)*
-
{*
-
if (!m_wasCrouching)*
-
{*
-
m_wasCrouching = true;*
-
OnCrouchEvent.Invoke(true);*
-
}*
move *= m_CrouchSpeed;
-
if (m_CrouchDisableCollider != null)*
-
m_CrouchDisableCollider.enabled = false;*
-
} else*
-
{*
-
if (m_CrouchDisableCollider != null)*
-
m_CrouchDisableCollider.enabled = true;*
-
if (m_wasCrouching)*
-
{*
-
m_wasCrouching = false;*
-
OnCrouchEvent.Invoke(false);*
-
}*
-
}*
Vector3 targetVelocity = new Vector2(move * 10f, m_Rigidbody2D.velocity.y);
-
m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref m_Velocity, m_MovementSmoothing);*
-
if (move > 0 && !m_FacingRight)*
-
{*
-
Flip();*
-
}*
-
else if (move < 0 && m_FacingRight)*
-
{*
-
Flip();*
-
}*
-
}*
-
if (m_Grounded && jump)*
-
{*
-
m_Grounded = false;*
-
m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));*
-
}*
-
}*
-
private void Flip()*
-
{*
-
m_FacingRight = !m_FacingRight;*
-
Vector3 theScale = transform.localScale;*
_ theScale.x *= -1;_
-
transform.localScale = theScale;*
- }*
}
Please help!
Note: I changed it to make the new vector2 into a “add force” but it still didn’t work so here’s new code
private void Update() - {*
-
isTouchingFront = Physics2D.OverlapCircle(FrontCheck.position, k_FrontRadius, m_WhatIsGround);*
-
if (Input.GetKey("right"))*
-
{*
-
m_lastHorizontal = 1;*
-
}*
-
else if (Input.GetKey("left"))*
-
{*
-
m_lastHorizontal = -1;*
-
}*
-
if (isTouchingFront == true && m_Grounded == false)*
-
{*
-
wallSliding = true;*
-
}*
-
else*
-
{*
-
wallSliding = false;*
-
}*
-
if (wallSliding)*
-
{*
m_Rigidbody2D.velocity = new Vector2(m_Rigidbody2D.velocity.x, Mathf.Clamp(m_Rigidbody2D.velocity.y, -wallSlidingSpeed, Input.GetAxisRaw(“Horizontal”) * 4));
-
}*
-
if (Input.GetButtonDown("Jump") && wallSliding == true)*
-
{*
-
wallJumping = true;*
-
Invoke("SetJumpToFalse", wallJumpTime);*
-
}*
-
else*
-
{*
-
wallJumping = false;*
-
}*
-
if (wallJumping)*
-
{*
-
print("jumping!!!!");*
m_Rigidbody2D.AddForce(new Vector2(xWallForce * -m_lastHorizontal, yWallForce), ForceMode2D.Impulse);
-
wallJumping = false;*
-
}*
- }*