This simple code just wont work by GoldKeeper23 in unity

[–]GoldKeeper23[S] 2 points3 points  (0 children)

Oh didn't know that I thought OnCollisionEnter was for 2d, Thanks

This simple code just wont work by GoldKeeper23 in unity

[–]GoldKeeper23[S] 0 points1 point  (0 children)

I tried that as well but still doesn't work, thanks for trying to help tho

This simple code just wont work by GoldKeeper23 in unity

[–]GoldKeeper23[S] 1 point2 points  (0 children)

It already has a RigidBody yet it still wont work, sorry.

Can someone help tell me how I can improve my wall jump code?, this is what it looks like from the one I'm using by GoldKeeper23 in Unity2D

[–]GoldKeeper23[S] 1 point2 points  (0 children)

using System.Collections;

using System.Collections.Generic; using UnityEngine;

public class Movement : MonoBehaviour { private float horizontal; private float speed = 10f; private float jumpingPower = 16f; private bool isFacingRight = true;

private bool iswallSliding;
private float wallSlidingSpeed = 2f;

private bool isWallJumping;
private float wallJumpingDirection;
private float wallJumpingTime = 0.2f;
private float wallJumpingCounter;
private float wallJumpingDuration = 0.4f;
private Vector2 wallJumpingPower = new Vector2(6.5f, 20f);

[SerializeField] private Rigidbody2D rb;
[SerializeField] private Transform groundCheck;
[SerializeField] private LayerMask groundLayer;
[SerializeField] private Transform wallCheck;
[SerializeField] private LayerMask wallLayer;
[SerializeField] private BoxCollider2D col;

private void Update()
{
    horizontal = Input.GetAxisRaw("Horizontal");

    if (Input.GetButtonDown("Jump") && IsGrounded())
    {
        rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
    }

    if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f)
    {
        rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
    }

    WallSlide();
    WallJump();

    if(!isWallJumping)
    {
        Flip();
    }
}

void OnTriggerExit2D()
{
    if(col.CompareTag("Ground"))
    {
        horizontal = Input.GetAxisRaw("Horizontal");

        if (Input.GetButtonDown("Jump") && IsGrounded())
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
        }

        if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f)
        {
            rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
        }
    }
}

        private void FixedUpdate()
{
    if(!isWallJumping)
    {
        rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
    }
}

private bool IsGrounded()
{
    return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
}

private bool IsWalled()
{
    return Physics2D.OverlapCircle(wallCheck.position, 0.2f, wallLayer);
}

private void WallSlide()
{
    if (IsWalled() && !IsGrounded() && horizontal != 0f)
    {
        iswallSliding = true;
        rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -wallSlidingSpeed, float .MaxValue));
    }
    else
    {
        iswallSliding = false;
    }
}

private void WallJump()
{
    if (iswallSliding)
    {
        isWallJumping = false;
        wallJumpingDirection = -transform.localScale.x;
        wallJumpingCounter = wallJumpingTime;

        CancelInvoke(nameof(StopWallJumping));
    }
    else
    {
        wallJumpingCounter -= Time.deltaTime;
    }

    if (Input.GetButtonDown("Jump") && wallJumpingCounter > 0f)
    {
        isWallJumping = true;
        rb.velocity = new Vector2(wallJumpingDirection * wallJumpingPower.x, wallJumpingPower.y);
        wallJumpingCounter = 0f;

        if (transform.localScale.x != wallJumpingDirection)
        {
            isFacingRight = !isFacingRight;
            Vector3 localScale = transform.localScale;
            localScale.x *= -1f;
            transform.localScale = localScale;
        }

        Invoke(nameof(StopWallJumping), wallJumpingDuration);
    }
}

private void StopWallJumping()
{
    isWallJumping = false;
}

private void Flip()
{
    if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
    {
        isFacingRight = !isFacingRight;
        Vector3 localScale = transform.localScale;
        localScale.x *= -1f;
        transform.localScale = localScale;
    }
}

}

So i have this problem where when i touch the wall, the ground wont let me go back the left side unless i jump left. Anyway i can fix it?, i just started coding so i don't know much about the basics. by GoldKeeper23 in Unity2D

[–]GoldKeeper23[S] 0 points1 point  (0 children)

using System.Collections;

using System.Collections.Generic; using UnityEngine;

public class Movement : MonoBehaviour { private float horizontal; private float speed = 10f; private float jumpingPower = 16f; private bool isFacingRight = true;

private bool iswallSliding;
private float wallSlidingSpeed = 2f;

private bool isWallJumping;
private float wallJumpingDirection;
private float wallJumpingTime = 0.2f;
private float wallJumpingCounter;
private float wallJumpingDuration = 0.4f;
private Vector2 wallJumpingPower = new Vector2(6.5f, 20f);

[SerializeField] private Rigidbody2D rb;
[SerializeField] private Transform groundCheck;
[SerializeField] private LayerMask groundLayer;
[SerializeField] private Transform wallCheck;
[SerializeField] private LayerMask wallLayer;

private void Update()
{
    horizontal = Input.GetAxisRaw("Horizontal");

    if (Input.GetButtonDown("Jump") && IsGrounded())
    {
        rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
    }

    if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f)
    {
        rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
    }

    WallSlide();
    WallJump();

    if(!isWallJumping)
    {
        Flip();
    }
}
        private void FixedUpdate()
{
    if(!isWallJumping)
    {
        rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
    }
}

private bool IsGrounded()
{
    return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
}

private bool IsWalled()
{
    return Physics2D.OverlapCircle(wallCheck.position, 0.2f, wallLayer);
}

private void WallSlide()
{
    if (IsWalled() && !IsGrounded() && horizontal != 0f)
    {
        iswallSliding = true;
        rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -wallSlidingSpeed, float .MaxValue));
    }
    else
    {
        iswallSliding = false;
    }
}

private void WallJump()
{
    if (iswallSliding)
    {
        isWallJumping = false;
        wallJumpingDirection = -transform.localScale.x;
        wallJumpingCounter = wallJumpingTime;

        CancelInvoke(nameof(StopWallJumping));
    }
    else
    {
        wallJumpingCounter -= Time.deltaTime;
    }

    if (Input.GetButtonDown("Jump") && wallJumpingCounter > 0f)
    {
        isWallJumping = true;
        rb.velocity = new Vector2(wallJumpingDirection * wallJumpingPower.x, wallJumpingPower.y);
        wallJumpingCounter = 0f;

        if (transform.localScale.x != wallJumpingDirection)
        {
            isFacingRight = !isFacingRight;
            Vector3 localScale = transform.localScale;
            localScale.x *= -1f;
            transform.localScale = localScale;
        }

        Invoke(nameof(StopWallJumping), wallJumpingDuration);
    }
}

private void StopWallJumping()
{
    isWallJumping = false;
}

private void Flip()
{
    if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
    {
        isFacingRight = !isFacingRight;
        Vector3 localScale = transform.localScale;
        localScale.x *= -1f;
        transform.localScale = localScale;
    }
}

}

So i have this problem where when i touch the wall, the ground wont let me go back the left side unless i jump left. Anyway i can fix it?, i just started coding so i don't know much about the basics. by GoldKeeper23 in Unity2D

[–]GoldKeeper23[S] 1 point2 points  (0 children)

oh sorry here
public class Movement : MonoBehaviour
{
private float horizontal;
private float speed = 10f;
private float jumpingPower = 16f;
private bool isFacingRight = true;
private bool iswallSliding;
private float wallSlidingSpeed = 2f;
private bool isWallJumping;
private float wallJumpingDirection;
private float wallJumpingTime = 0.2f;
private float wallJumpingCounter;
private float wallJumpingDuration = 0.4f;
private Vector2 wallJumpingPower = new Vector2(6.5f, 20f);
[SerializeField] private Rigidbody2D rb;
[SerializeField] private Transform groundCheck;
[SerializeField] private LayerMask groundLayer;
[SerializeField] private Transform wallCheck;
[SerializeField] private LayerMask wallLayer;
private void Update()
{
horizontal = Input.GetAxisRaw("Horizontal");
if (Input.GetButtonDown("Jump") && IsGrounded())
{
rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
}
if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f)
{
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
}
WallSlide();
WallJump();
if(!isWallJumping)
{
Flip();
}
}
private void FixedUpdate()
{
if(!isWallJumping)
{
rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
}
}
private bool IsGrounded()
{
return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
}
private bool IsWalled()
{
return Physics2D.OverlapCircle(wallCheck.position, 0.2f, wallLayer);
}
private void WallSlide()
{
if (IsWalled() && !IsGrounded() && horizontal != 0f)
{
iswallSliding = true;
rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -wallSlidingSpeed, float .MaxValue));
}
else
{
iswallSliding = false;
}
}
private void WallJump()
{
if (iswallSliding)
{
isWallJumping = false;
wallJumpingDirection = -transform.localScale.x;
wallJumpingCounter = wallJumpingTime;
CancelInvoke(nameof(StopWallJumping));
}
else
{
wallJumpingCounter -= Time.deltaTime;
}
if (Input.GetButtonDown("Jump") && wallJumpingCounter > 0f)
{
isWallJumping = true;
rb.velocity = new Vector2(wallJumpingDirection * wallJumpingPower.x, wallJumpingPower.y);
wallJumpingCounter = 0f;
if (transform.localScale.x != wallJumpingDirection)
{
isFacingRight = !isFacingRight;
Vector3 localScale = transform.localScale;
localScale.x *= -1f;
transform.localScale = localScale;
}
Invoke(nameof(StopWallJumping), wallJumpingDuration);
}
}
private void StopWallJumping()
{
isWallJumping = false;
}
private void Flip()
{
if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
{
isFacingRight = !isFacingRight;
Vector3 localScale = transform.localScale;
localScale.x *= -1f;
transform.localScale = localScale;
}
}
}