- Home /
character can jump, then isnt able to when I walk. Unity 2d
So here's the problem. I start the game up, and it's fine, my character can run and jump willy nilly. But then after a couple seconds of playing, the character can barely jump while its running, only jumping half the time. Idk what's happening, but if anyone could find potential flaws in my code that might be contributing to this, help would be appreciated. The scripts below are my movement and ground check scripts. also bear in mind that I do have walking animations and idle animations implemented, but I turned them off and it didn't do anything.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move2D : MonoBehaviour
{
public float moveSpeed = 5f;
public bool isGrounded = false;
public bool currentjc = false;
public Animator animator;
public float cs;
private void FixedUpdate()
{
}
// Update is called once per frame
void Update()
{
jump();
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
transform.position += movement * Time.deltaTime * moveSpeed;
animator.SetFloat("playerSpeed", Mathf.Abs(Input.GetAxis("Horizontal") ));
cs = Input.GetAxis("Horizontal") * moveSpeed;
}
void jump()
{
if (Input.GetButtonDown("Jump") && isGrounded == true )
{
gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 100.5f), ForceMode2D.Impulse);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class gc : MonoBehaviour
{
GameObject Player;
void Start()
{
Player = gameObject.transform.parent.gameObject;
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.collider.tag == "Ground")
{
Player.GetComponent<Move2D>().isGrounded = true;
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.collider.tag == "Ground")
{
Player.GetComponent<Move2D>().isGrounded = false;
}
}
}
Here is an unlisted youtube video showing off the problem. https://youtu.be/DWEoyY2UU0c Any help is appreciated!
Answer by DTheChemist · Jan 16 at 09:13 AM
the Is_Grounded is part of the ground check detection public bool of mines. Just adjust your own
void Update() {
if (Input.GetButtonDown("Jump") && Is_Grounded()) {
Jump();
}
}
void Jump() { Vector2 movement = new Vector2(rb.velocity.x, jumpForce);
rb.velocity = movement;
}