First coding project here. But I know the basics and it looks like I’m doing everything right, but clearly I’m not
Basically the player will jump off of either left or right walls perfectly fine, but never both. It all depends on whether I have the “x” variable in the new vector2 set to negative or positive. Also whether I set the player to facing right or left.
So I figured, easy fix, I’ll just make it so if the player is facing left, it’s negative, or else it’s positive. But it doesn’t work. Thanks
Here’s the if statement that enables the player to only jump off of left walls:
if (m_FacingRight == true)
{
rb.velocity = new Vector2(20f, 20f);
}
else
{
rb.velocity = new Vector2(-20f, 20f);
}
And here’s the entire Player Movement script (most wall jumping code is at the start of the update function, with player movement in the Fixed update and some at the very bottom)
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 = 800f;
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 2
// [Header("For WallSliding")]
// [SerializeField] float wallSlideSpeed = 0f;
// [SerializeField] LayerMask wallLayer;
// [SerializeField] Transform wallCheckPoint;
// [SerializeField] Vector2 wallCheckSize;
// public bool isTouchingWall;
// private bool isWallSliding;
//walljump stuff
public Transform wallGrabPoint;
private bool canGrab, isGrabbing;
private float gravityStore;
public float wallJumpTime = .2f;
private float wallJumpCounter;
[SerializeField] LayerMask wallLayer;
private bool m_FacingRight = true;
float move;
private Vector3 m_Velocity = Vector3.zero;
[Range(0, .3f)] [SerializeField] private float m_MovementSmoothing = .05f;
void Start()
{
//wall jump stuff
gravityStore = rb.gravityScale;
}
void Update()
{
//if(wallJumpCounter <= 0)
Vector3 targetVelocity = new Vector2(move * 10f, rb.velocity.y);
{
//handle wall jumping
canGrab = Physics2D.OverlapCircle(wallGrabPoint.position, .2f, wallLayer);
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;
if(Input.GetButtonDown("Jump"))
{
StartCoroutine("DisableScript");
//wallJumpCounter = wallJumpTime;
if (m_FacingRight == true)
{
rb.velocity = new Vector2(20f, 20f);
}
else
{
rb.velocity = new Vector2(-20f, 20f);
}
//rb.AddForce(transform.right * 1000f, ForceMode2D.Impulse);
Debug.Log("Imwalljumping");
rb.gravityScale = gravityStore;
isGrabbing = false;
}
} else{
rb.gravityScale = gravityStore;
}
animator.SetBool("isGrabbing", isGrabbing);
{
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;
}
}
//else
// {
// wallJumpCounter -= Time.deltaTime;
// }
}
// void WallSlide()
// {
// if (isTouchingWall && !m_Grounded && rb.velocity.y < 0f)
// {
// isWallSliding = true;
// }
// else{
// isWallSliding = false;
// }
// //wall slide
// if (isWallSliding)
// {
// rb.velocity = new Vector2(rb.velocity.x, wallSlideSpeed);
// }
// }
// void CheckWorld()
// {
// isTouchingWall = Physics2D.OverlapBox(wallCheckPoint.position, wallCheckSize, 0f, wallLayer);
// }
// private void OnDrawGizmosSelected()
// {
// Gizmos.color = Color.red;
// Gizmos.DrawCube(wallCheckPoint.position, wallCheckSize);
// }
public void OnLanding ()
{
animator.SetBool("IsJumping", false);
}
public void OnCrouching (bool isCrouching)
{
animator.SetBool("IsCrouching", isCrouching);
}
void FixedUpdate()
{
// WallSlide();
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);
// }
// }
IEnumerator DisableScript ()
{
GameObject.Find("Player").GetComponent<PlayerMovement>().enabled = false;
yield return new WaitForSeconds(.1f);
GameObject.Find("Player").GetComponent<PlayerMovement>().enabled = true;
}
public void Move(float move, bool crouch, bool jump)
{
// Move the character by finding the target velocity
Vector3 targetVelocity = new Vector2(move * 10f, rb.velocity.y);
// And then smoothing it out and applying it to the character
rb.velocity = Vector3.SmoothDamp(rb.velocity, targetVelocity, ref m_Velocity, m_MovementSmoothing);
// If the input is moving the player right and the player is facing left...
if (move > 0 && !m_FacingRight)
{
// ... flip the player.
Flip();
}
// Otherwise if the input is moving the player left and the player is facing right...
else if (move < 0 && m_FacingRight)
{
// ... flip the player.
Flip();
}
}
private void Flip()
{
// Switch the way the player is labelled as facing.
m_FacingRight = !m_FacingRight;
transform.Rotate(0f, 180f, 0f);
}
}