I want to make a jump script with responsive jumping, button tapping and holding, coyote time etc. I am trying to implement a way to make the player jump lower when tapping the jump button than when holding it down.
The basic principle i know of is to increase the downward velocity of the player when the jump button is released but the rb.velocity.y > 0f.
My problem is that i am just learning how to use the new input system so i dunno how to do that
Here is my full script…
using System.Net.Http.Headers;
using UnityEditor.Timeline;
using UnityEngine;
using UnityEngine.InputSystem;
using static UnityEditor.Timeline.TimelinePlaybackControls;
public class MovementScript2D : MonoBehaviour
{
[Header("Jumping")]
[SerializeField] float jumpHeight;
[Tooltip("The number by which we will multiply gravity when character is falling down to make the jump feel better")]
[SerializeField] float fallMultiplier;
[Tooltip("The number by which the gravity is multiplied when NOT holding down the jumpButton")]
[SerializeField] float lowJumpMultiplier = 2f;
[Header("Movement")]
[Tooltip("The maximum speed of the character")]
[SerializeField] float maxMovementSpeed;
[Tooltip("Distance from the orign of the gameObject and the raycast being casted for the groundCheck")]
[SerializeField] float distanceOfRayCast = 2f;
[SerializeField] float acceleration;
[Header("Miscallaneous")]
[SerializeField] LayerMask groundLayer;
[SerializeField] Color gizmoColor = Color.red;
Rigidbody2D rb;
InputSystem inputSystem;
bool onGround;
// Bool to check the direction in which the player is facing
bool isFacingRight;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
InitializeInputSystem();
}
private void Update()
{
onGround = Physics2D.Raycast(transform.position, Vector3.down, distanceOfRayCast, groundLayer);
}
private void FixedUpdate()
{
HandleMovement();
speedCap();
if (rb.velocity.y < 0)
{
applyFallMultiplier();
}
}
private void InitializeInputSystem()
{
inputSystem = new InputSystem();
inputSystem.Ground.Jump.Enable();
inputSystem.Ground.Jump.performed += HandleJump;
inputSystem.Ground.Move.Enable();
// inputSystem.Ground.Move.performed += Move;
}
#region Movement Methods
private void HandleMovement()
{
Vector2 inputMovementVector = inputSystem.Ground.Move.ReadValue<Vector2>();
float targetSpeed = inputMovementVector.x * maxMovementSpeed * Time.fixedDeltaTime;
float currentSpeed = rb.velocity.x;
// The player will have this amount of acceleration. Changes with time
float accelerationFactor = Mathf.Clamp01(acceleration * Time.fixedDeltaTime);
// Linearly increase from currentSpeed to targetSpeed by the accelerationFactor. accelerartionFactor changes with time
float finalSpeed = Mathf.Lerp(currentSpeed, targetSpeed, accelerationFactor);
rb.velocity = new Vector2(finalSpeed, rb.velocity.y);
}
void speedCap()
{
// Capping the speed from -maxMovementSpeed to maxMovementSpeed. This function returns the present value if it lies in the given range
float cappedSpeed = Mathf.Clamp(rb.velocity.x, -maxMovementSpeed, maxMovementSpeed);
rb.velocity = new Vector2(cappedSpeed, rb.velocity.y);
}
#endregion
#region Jump Methods
private void HandleJump(InputAction.CallbackContext context)
{
if (context.performed && onGround)
{
rb.AddForce(Vector2.up * jumpHeight, ForceMode2D.Impulse);
}
if(rb.velocity.y > 0f && context.canceled)
{
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y);
rb.AddForce(Vector2.down * Physics2D.gravity.y * (lowJumpMultiplier - 1), ForceMode2D.Force);
}
}
void applyFallMultiplier()
{
rb.AddForce(Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1), ForceMode2D.Force);
}
private void OnDrawGizmos()
{
Gizmos.color = gizmoColor;
Gizmos.DrawLine(transform.position, transform.position + Vector3.down * distanceOfRayCast);
}
#endregion
}
Here is only the jump function…
#endregion
#region Jump Methods
private void HandleJump(InputAction.CallbackContext context)
{
if (context.performed && onGround)
{
rb.AddForce(Vector2.up * jumpHeight, ForceMode2D.Impulse);
}
if(rb.velocity.y > 0f && context.canceled)
{
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y);
rb.AddForce(Vector2.down * Physics2D.gravity.y * (lowJumpMultiplier - 1), ForceMode2D.Force);
}
}
void applyFallMultiplier()
{
rb.AddForce(Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1), ForceMode2D.Force);
}
private void OnDrawGizmos()
{
Gizmos.color = gizmoColor;
Gizmos.DrawLine(transform.position, transform.position + Vector3.down * distanceOfRayCast);
}
#endregion
if(rb.velocity.y > 0f && context.canceled)
{
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y);
rb.AddForce(Vector2.down * Physics2D.gravity.y * (lowJumpMultiplier - 1), ForceMode2D.Force);
}