I’ve been coding a 2D platformer, and it all works except one thing. A jump lasts longer if the player is also moving, it also goes higher. I’m having trouble understanding how this problem is even happening as nothing in the movement script is effecting the Y velocity and vice versa for the Jumping script. Any help appreciated thanks!
Player Movement Script:
```csharp
*using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[Header("components")]
private Rigidbody2D rb;
[Header("Movement Variables")]
[SerializeField] private float movementAcceleration;
[SerializeField] private float maxMoveSpeed;
[SerializeField] private float linearDrag;
private float horizontalDirection;
private bool changingDirection => (rb.velocity.x > 0f && horizontalDirection < 0f) || (rb.velocity.x < 0f && horizontalDirection > 0f);
// Start is called before the first frame update
private void Start()
{
rb = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
MoveCharacter();
ApplyLinearDrag();
}
// Update is called once per frame
private void Update()
{
horizontalDirection = GetInput().x;
}
private Vector2 GetInput()
{
return new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
}
private void MoveCharacter()
{
rb.AddForce(new Vector2(horizontalDirection, 0f) * movementAcceleration);
if (Mathf.Abs(rb.velocity.x) > maxMoveSpeed)
rb.velocity = new Vector2(Mathf.Sign(rb.velocity.x) * maxMoveSpeed, rb.velocity.y);
}
private void ApplyLinearDrag()
{
if(Mathf.Abs(horizontalDirection) < 0.4f || changingDirection)
{
rb.drag = linearDrag;
}
else
{
rb.drag = 0f;
}
}
}*
* _**PlayerJump Script:**_ *
csharp
*using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerJump : MonoBehaviour
{
private Rigidbody2D rb;
// JUMP VARIABLES
private bool isGrounded;
public Transform feetPos;
public float checkRadius;
public float jumpForce;
public LayerMask whatIsGround;
//CANNOT REPEAT JUMPS
private float jumpTimeCounter;
public float jumpTime;
private bool isJumping;
// AIR DRAG
[SerializeField] private float airLinearDrag = 2.5f;
// FALLING
[SerializeField] private float fallMultiplier = 8f;
[SerializeField] private float lowJumpFallMultiplier = 5f;
//HangTime extra good stuff
public float hangTime = .2f;
private float hangCounter;
//Jump buffer stuff
public float jumpBufferLength = .1f;
private float jumpBufferCount;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate()
{
FallMultiplier();
}
// Update is called once per frame
void Update()
{
isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
// Hang Time Block
if(isGrounded)
{
hangCounter = hangTime;
} else
{
hangCounter -= Time.deltaTime;
}
//manage jump buffer
if(Input.GetButtonDown("Jump"))
{
jumpBufferCount = jumpBufferLength;
} else
{
jumpBufferCount -= Time.deltaTime;
}
// jump
if (hangCounter > 0f && jumpBufferCount >= 0)
{
isJumping = true;
jumpTimeCounter = jumpTime;
rb.velocity = Vector2.up * jumpForce;
jumpBufferCount = 0;
}
if (Input.GetButton("Jump") && isJumping == true)
{
if (jumpTimeCounter > 0)
{
rb.velocity = new Vector2(rb.velocity.x, 0f);
rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
jumpTimeCounter -= Time.deltaTime;
}
else
{
isJumping = false;
}
}
if (Input.GetButtonUp("Jump") && isJumping == true)
{
isJumping = false;
}
if (isGrounded == false)
{
ApplyAirLinearDrag();
}
}
private void ApplyAirLinearDrag()
{
rb.drag = airLinearDrag;
}
private void FallMultiplier()
{
if (rb.velocity.y < 0)
{
rb.gravityScale = fallMultiplier;
}
else if (rb.velocity.y > 0 && !Input.GetButton("Jump"))
{
rb.gravityScale = lowJumpFallMultiplier;
}
else
{
rb.gravityScale = 1f;
}
}
}*
```