I did this basic movement script and sometimes my player jumps very high, for no reason I can find. A normal jump, go around 1.2 up on the y axis, while the higher jump goes up 7 on the y.
My player is being organized by one parent game object with the rigidbody2d and player script, 2 children gameobject with sprite and collision detection respectively
My player script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
//Player movement script
float xInput;
bool jumpInput;
Rigidbody2D rg;
[Header("Variables Editor")]
[SerializeField] internal int jumpMax, jumpCount;
[SerializeField] private float forceJump, walkForce;
void GetInput()
{
xInput = Input.GetAxisRaw("Horizontal");
jumpInput = Input.GetKeyDown(KeyCode.Space);
}
void Move()
{
rg.velocity = new Vector2(xInput * walkForce * Time.deltaTime, rg.velocity.y);
}
void Jump()
{
jumpCount++;
if (jumpCount < jumpMax)
{
//jump
Debug.Log("Pulo");
rg.AddForce(Vector2.up * forceJump * Time.deltaTime, ForceMode2D.Impulse);
}
else
{
jumpCount--;
return;
//dont jump
}
}
private void Awake()
{
rg = GetComponent<Rigidbody2D>();
}
private void Update()
{
GetInput();
}
private void FixedUpdate()
{
if (jumpInput)
{
Jump();
}
Move();
}
}