Hey guys.
So i’m setting this code for this 2D platformer where you are basically locked inside 4 walls each level.
I have it set up so my player can jump once when he touches a wall and even falls down after some time of being wall sliding but what i need is a way to tell me code that the player can’t jump using the x velocity to the side of the wall, if he does this, he should simply jump straight.
It would be a fix because if not he does a super small gltich/bounce thing against the wall he is already on.
I leave the code, and let me know if you have any idea how i could approach this ty!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField] private Rigidbody2D theRB;
[SerializeField] private float moveSpeed;
[SerializeField] private float jumpForce;
private bool hasTouchedGround;
[SerializeField] private GameObject groundPoint;
[SerializeField] private LayerMask WhatIsGround;
[SerializeField] Animator anim;
private bool isWall;
private bool hasTouchedWall;
private Vector2 initialForce;
[SerializeField] private float maxJumpSpeed;
private bool allowJump;
//setting falling down checks
private float currentHeight;
private bool isFalling;
[SerializeField] private float wallFallingSpeed;
private Material RbMaterial;
private bool isJump = false;
private float initialForceY = 5;
private bool canWallJump;
// private void Awake()
// {
// theRB.sharedMaterial.bounciness = 1;
// }
void Start()
{
isWall = false;
hasTouchedWall = false;
allowJump = false;
hasTouchedGround = true;
anim.SetBool(“touchingWall”, false);
}
void Update()
{
//check for ground;
if (Physics2D.OverlapCircle(groundPoint.transform.position, .2f, WhatIsGround) == true)
{
hasTouchedGround = true;
theRB.sharedMaterial.bounciness = 1;
}
//setting Jump
if (Input.GetButtonDown(“Jump”))
{
isJump = true;
}
//check for falling
// if (transform.position.y < currentHeight && !isJump)
// {
// isFalling = true;
// }
// if (transform.position.y > currentHeight && isJump)
// {
// isFalling = false;
// }
Debug.Log("is touching wall " + isWall);
// Debug.Log(theRB.sharedMaterial.bounciness);
// Debug.Log("can i jump? " + isJump);
//handling direction change, THIS WAS CAUSING ISSUED WITH WALL BOUNCINESS
// if (theRB.velocity.x < 0)
// {
// transform.localScale = new Vector3(-1f, 1f, 1f);
// }
// if (theRB.velocity.x > 0)
// {
// transform.localScale = new Vector3(1f, 1f, 1f);
// }
//Setting a max velocity for the Y axis
if (theRB.velocity.y > maxJumpSpeed)
{
theRB.velocity = new Vector2(theRB.velocity.x, maxJumpSpeed);
}
if (isJump)
{
if (hasTouchedGround)
{
theRB.sharedMaterial.bounciness = 1;
theRB.velocity = new Vector2(Input.GetAxisRaw(“Horizontal”) * moveSpeed, jumpForce);
isWall = false;
canWallJump = true;
}
}
//movevement sideways
if (!isWall)
{
theRB.sharedMaterial.bounciness = 1;
theRB.velocity = new Vector2(Input.GetAxisRaw(“Horizontal”) * moveSpeed, theRB.velocity.y);
if (isJump && hasTouchedGround)
{
Jump();
hasTouchedGround = false;
}
}
if (isWall)
{
theRB.velocity = new Vector2(0, wallFallingSpeed);
theRB.sharedMaterial.bounciness = 0;
StartCoroutine(fallTimer());
if (isJump && canWallJump)
{
isWall = false;
Jump();
// isFalling = false;
theRB.velocity = new Vector2(Input.GetAxisRaw(“Horizontal”) * moveSpeed, jumpForce);
canWallJump = false;
}
}
//setting animations
anim.SetFloat(“speed”, Mathf.Abs(theRB.velocity.x));
}
void FixedUpdate()
{
// setting the initial bouncing force
initialForce = new Vector2(0, 5);
theRB.AddForce(initialForce);
}
private void LateUpdate()
{
currentHeight = transform.position.y;
}
//checking for walls
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == “Walls”)
{
isWall = true;
hasTouchedWall = true;
// anim.SetBool(“touchingWall”, true);
}
}
void Jump()
{
if (isJump)
{
theRB.velocity = new Vector2(theRB.velocity.x, jumpForce);
}
isJump = false;
hasTouchedGround = false;
}
IEnumerator fallTimer()
{
yield return new WaitForSeconds(1f);
Debug.Log(“coroutine succesfull”);
isWall = false;
}
}