Hi, I’m trying to get my player to wall jump. He grabs on to walls that are on his right just fine, but not walls that are on his left. Thanks for any help.
Also, this is my first project so feel free to spell it out for me.
I’m about to go out for tonight but will respond tomorrow morning!
My player movement code. (The wall jumping code is labelled, and at the start of the update function)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class PlayerMovement : MonoBehaviour
{
public CharacterController2D controller;
public Animator animator;
public float runSpeed = 40f;
float horizontalMove = 0f;
bool jump = false;
bool crouch = false;
[SerializeField] private LayerMask m_WhatIsGround; // A mask determining what is ground to the character
[SerializeField] private Transform m_GroundCheck; // A position marking where to check if the player is grounded.
const float k_GroundedRadius = .2f;
private bool m_Grounded;
public UnityEvent OnLandEvent;
[SerializeField] private AudioSource jumpSoundEffect;
[SerializeField] private AudioSource dashSoundEffect;
// //dash stuff
[SerializeField] Rigidbody2D rb;
public float dashDistance = 10f;
bool isDashing;
float doubleTapTime;
KeyCode lastKeyCode;
float mx;
[Header("Dashing")]
[SerializeField] private float m_JumpForce = 400f;
public bool canDash = true;
public float dashingTime;
public float dashSpeed;
public float dashJumpIncrease;
public float timeBtwDashes;
//double tap stuff
public bool singleTap = false;
public bool doubleTap = false;
bool tapping = false;
float tapTime = 0;
float duration = .4f;
//walljump stuff
public Transform wallGrabPoint;
private bool canGrab, isGrabbing;
private float gravityStore;
void Start()
{
//wall jump stuff
gravityStore = rb.gravityScale;
}
void Update()
{
//handle wall jumping
canGrab = Physics2D.OverlapCircle(wallGrabPoint.position, .2f, m_WhatIsGround);
isGrabbing = false;
if(canGrab && !m_Grounded)
{
if((transform.localScale.x == 1f && Input.GetAxisRaw("Horizontal") > 0))
{
isGrabbing = true;
}
if((transform.localScale.x == -1f && Input.GetAxisRaw("Horizontal") < 0))
{
isGrabbing = true;
}
}
if(isGrabbing)
{
rb.gravityScale = 0f;
rb.velocity = Vector2.zero;
} else{
rb.gravityScale = gravityStore;
}
{
if (Input.GetKeyDown(KeyCode.LeftShift))
{
DashAbility();
}
}
//double tap stuff
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
if (tapping)
{
doubleTap = true;
Debug.Log("DoubleTap");
DashAbility();
tapping = false;
}
else
{
tapping = true;
tapTime = duration;
}
}
if (tapping)
{
tapTime = tapTime - Time.deltaTime;
if (tapTime <= 0)
{
tapping = false;
singleTap = true;
Debug.Log("SingleTap");
}
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
if (tapping)
{
doubleTap = true;
Debug.Log("DoubleTap");
DashAbility();
tapping = false;
}
else
{
tapping = true;
tapTime = duration;
}
}
if (tapping)
{
tapTime = tapTime - Time.deltaTime;
if (tapTime <= 0)
{
tapping = false;
singleTap = true;
Debug.Log("SingleTap");
}
}
horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
//testing move for wall jump
//rb.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * runSpeed, rb.velocity.y);
animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
if (m_Grounded && (Input.GetButtonDown("Jump")))
{
jump = true;
animator.SetBool("IsJumping", true);
jumpSoundEffect.Play();
}
if (Input.GetButtonDown("Crouch"))
{
crouch = true;
} else if (Input.GetButtonUp("Crouch"))
{
crouch = false;
}
}
public void OnLanding ()
{
animator.SetBool("IsJumping", false);
}
public void OnCrouching (bool isCrouching)
{
animator.SetBool("IsCrouching", isCrouching);
}
void FixedUpdate()
{
bool wasGrounded = m_Grounded;
m_Grounded = false;
// The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
// This can be done using layers instead but Sample Assets will not overwrite your project settings.
Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
{
m_Grounded = true;
if (!wasGrounded)
OnLandEvent.Invoke();
}
}
//Move our character
controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
jump = false;
}
public void StartWalking()
{
runSpeed = 50f;
}
public void StopWalking()
{
runSpeed = 0f;
}
// IEnumerator Dash (float direction)
// {
// isDashing = true;
// rb.velocity = new Vector2(rb.velocity.x, 0f);
// rb.AddForce(new Vector2(dashDistance * direction, 0f), ForceMode2D.Impulse);
// float gravity = rb.gravityScale;
// rb.gravityScale = 0f;
// yield return new WaitForSeconds(0.4f);
// isDashing = false;
// rb.gravityScale = gravity;
// }
void DashAbility()
{
if(canDash)
{
animator.SetBool("isDashing", true);
StartCoroutine(Dash());
}
}
IEnumerator Dash()
{
dashSoundEffect.Play();
canDash = false;
runSpeed = dashSpeed;
m_JumpForce = dashJumpIncrease;
yield return new WaitForSeconds(dashingTime);
animator.SetBool("isDashing", false);
runSpeed = 50f;
m_JumpForce = 800f;
yield return new WaitForSeconds(timeBtwDashes);
canDash = true;
}
void LateUpdate()
{
if (doubleTap) doubleTap = false;
if (singleTap) singleTap = false;
}
// void Animation()
// {
// if (isDashing)
// {
// animator.SetBool("isDashing", true);
// }
// else {
// animator.SetBool("isDashing", false);
// }
// }
}