- Home /
Problems with Jumping
Hello everyone,
I'm trying to code a jump for my character, but I've had some problems: I'm not able to find the right configuration for gravity, it's too slow, and I think that there aren't problems with scale. Baside that, something strange happens: if I try to jump while I'm running it jumps (slower than I'd like), but if I jump while the character is in idle, you can't even see how it jumps, just the start of the animation.
I'm trying to get a fast jump, but I only got a very slow jumo that doesn't work well. I appreciate any help. Here you have my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float maxSpeed = 25f; //El maximo en la práctica es 10 (dnw)
public float speed = 500f;
public float jumpPower = 30000000f;
public bool grounded;
private Rigidbody2D rb2d;
private Animator anim;
private bool jumping;
// Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
anim.SetFloat("Speed", Mathf.Abs(rb2d.velocity.x));
anim.SetBool("Grounded", grounded);
Debug.Log(grounded);
if (Input.GetKeyDown("joystick button 0") && grounded)
{
jumping = true;
Debug.Log("hola");
}
}
void FixedUpdate()
{
//MOVEMENT
float h = Input.GetAxis("LeftJoystickHorizontal");
rb2d.AddForce(Vector2.right * speed * h, ForceMode2D.Impulse);
float limitedSpeed = Mathf.Clamp(rb2d.velocity.x, -maxSpeed, maxSpeed);
rb2d.velocity = new Vector2(limitedSpeed, rb2d.velocity.y);
if (Mathf.Abs(h) < 0.1f){rb2d.velocity = Vector2.zero;};
if(h > 0.1f){transform.localScale = new Vector3(1f, 1f, 1f);}
if(h < -0.1f){transform.localScale = new Vector3(-1f, 1f, 1f);}
if(jumping == true)
{
rb2d.AddForce(Vector2.up * jumpPower, ForceMode2D.Impulse);
jumping = false;
}
//
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CheckGround : MonoBehaviour {
private PlayerController player;
// Use this for initialization
void Start () {
player = GetComponentInParent<PlayerController>();
}
// Update is called once per frame
void OnCollisionStay2D(Collision2D col) {
if(col.gameObject.tag == "Ground")
{
player.grounded = true;
}
}
void OnCollisionExit2D(Collision2D col)
{
if (col.gameObject.tag == "Ground")
{
player.grounded = false;
}
}
}
Thanks.
Your answer
Follow this Question
Related Questions
my jump key works about once every 10 presses 1 Answer
3DBuzz Third Person Character Jumping And Landing Issue 0 Answers
Jumping animation? Moving through objects? Syncing jump animation? 2 Answers
Running animation not working and jumping isn't working. 0 Answers
Parameter 'Jump' does not exist 1 Answer