- Home /
While wall jumping, I'm losing the key down (it seems) in 2D.
Hi, this is an example.
I'm totally new to Unity. I tried to split the code in a way that makes sense using both Update
and FixedUpdate
where I keep the stuff regarding the physics.
I'm using the new input system as well. As you can see sometimes I cannot jump while on the wall, sometimes the jump kicks in later (like you see at the end of the video where I jump from the ground).
I'm not able to understand why. Any help would be appreciated. Here's the source code:
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour
{
public Rigidbody2D rigidBody;
public float moveSpeed, jumpForce;
public Transform groundCheckPoint;
public LayerMask whatisGround;
public Animator anim;
public Transform wallGrabPoint;
public Text text;
private bool isGrounded;
private bool isGrabbing;
private bool canGrab;
private Vector2 move;
private float originalGravity;
private bool isJumping;
private void Awake()
{
move = Vector2.zero;
originalGravity = rigidBody.gravityScale;
}
public void OnJump()
{
if (isGrounded || canGrab)
{
isJumping = true;
}
}
public void OnMove(InputValue input)
{
move = input.Get<Vector2>();
}
void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheckPoint.position, .2f, whatisGround);
canGrab = Physics2D.OverlapCircle(wallGrabPoint.position, .2f, whatisGround);
rigidBody.velocity = new Vector2(move.x * moveSpeed, rigidBody.velocity.y);
if (isJumping && !canGrab)
{
rigidBody.velocity = new Vector2(rigidBody.velocity.x, jumpForce);
isJumping = false;
}
isGrabbing = false;
if (canGrab && !isGrounded)
{
if ( (transform.localScale.x == 1f && move.x > 0) ||
(transform.localScale.x == -1f && move.x < 0) )
{
isGrabbing = true;
rigidBody.velocity = Vector2.zero;
if (isJumping)
{
var force = new Vector2(-250*(move.x * moveSpeed), 50*jumpForce);
rigidBody.AddForce(force);
isJumping = false;
}
} else
{
rigidBody.gravityScale = originalGravity;
}
}
}
private void Update()
{
FlipDirection();
anim.SetFloat("speed", Mathf.Abs(rigidBody.velocity.x));
anim.SetBool("isGrounded", isGrounded);
anim.SetBool("isGrabbing", isGrabbing);
UpdateStatus();
}
private void FlipDirection()
{
if (rigidBody.velocity.x > 0)
{
transform.localScale = Vector3.one;
}
else if (rigidBody.velocity.x < 0)
{
transform.localScale = new Vector3(-1f, 1, 1f);
}
}
void UpdateStatus()
{
text.text = string.Format("Velocity = {0}" +
"\ntransform = {1}" +
"\nisGrounded = {2}" +
"\nisGrabbing = {3}" +
"\nisJumping = {4}" +
"\nmove = {5}",
rigidBody.velocity.ToString("F0"),
transform.localScale.ToString("F0"),
isGrounded.ToString(),
isGrabbing.ToString(),
isJumping.ToString(),
move.ToString("F0"));
}
}
Your answer
Follow this Question
Related Questions
Projectile gets stuck at character's position (Unity2d) 0 Answers
trying to make an enemy shoot a projectile at the player when the player enters the enemys range 2 Answers
character can jump, then isnt able to when I walk. Unity 2d 1 Answer
When I build my 2D game and set it to fullscreen, the resolution looks off 0 Answers