Question by
kashifkamil · Apr 22, 2016 at 03:04 PM ·
inputjumpingplatformergrounded
How do I script my 2d platformer player controller so that there is a freedom of a few milliseconds when checking if grounded?
In my script, when the jump key is pressed, it checks if the player is grounded and only if so, it allows jumping. When playing games, there is a tendency to tap jump a few frames before actually being grounded. There is no airjump in this game. How do I have a small buffer time so that if the player taps jump a few milliseconds before grounded, that input carries forward to the frame where the player is grounded and then the character jumps.
This is my player script
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (Controller2D))]
public class Player : MonoBehaviour {
public float maxJumpHeight=4;
public float minJumpHeight=1;
public float timeToJumpApex=.4f;
float accelerationTimeAirborne=.2f;
float accelerationTimeGrounded=.1f;
float moveSpeed=6;
float runSpeed=6;
public Vector2 wallJumpClimb;
public Vector2 wallJumpOff;
public Vector2 wallLeap;
public float wallSlideSpeedMax=3;
public float wallStickTime = .25f;
float timeToWallUnstick;
float gravity;
float maxJumpVelocity;
float minJumpVelocity;
Vector3 velocity;
float velocityXSmoothing;
Controller2D controller;
void Start() {
controller = GetComponent<Controller2D> ();
gravity = -(2 * maxJumpHeight) / Mathf.Pow (timeToJumpApex, 2);
maxJumpVelocity = Mathf.Abs (gravity) * timeToJumpApex;
minJumpVelocity = Mathf.Sqrt (2 * Mathf.Abs (gravity) * minJumpHeight);
print ("Gravity" + gravity + "Jump Velocity" + maxJumpVelocity);
}
void Update() {
Vector2 input = new Vector2 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"));
int wallDirX=(controller.collisions.left)?-1:1;
if (Input.GetButton ("Sprint"))
runSpeed = 6;
else
runSpeed = 0;
float targetVelocityX = input.x * (moveSpeed+runSpeed);
velocity.x = Mathf.SmoothDamp (velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below)?accelerationTimeGrounded:accelerationTimeAirborne);
bool wallSliding = false;
if ((controller.collisions.left || controller.collisions.right) && !controller.collisions.below && velocity.y < 0)
{
wallSliding = true;
if (velocity.y < -wallSlideSpeedMax) {
velocity.y = -wallSlideSpeedMax;
}
if (timeToWallUnstick > 0) {
velocityXSmoothing = 0;
velocity.x = 0;
if (input.x != wallDirX && input.x != 0) {
timeToWallUnstick -= Time.deltaTime;
} else {
timeToWallUnstick = wallStickTime;
}
}
else {
timeToWallUnstick = wallStickTime;
}
}
if (Input.GetButtonDown("Jump")) //&& controller.collisions.below
{
if (wallSliding) {
if (wallDirX == input.x) {
velocity.x = -wallDirX * wallJumpClimb.x;
velocity.y = wallJumpClimb.y;
}
else if (input.x == 0) {
velocity.x = -wallDirX * wallJumpOff.x;
velocity.y = wallJumpOff.y;
}
else {
velocity.x = -wallDirX * wallLeap.x;
velocity.y = wallLeap.y;
}
}
if (controller.collisions.below) {
velocity.y = maxJumpVelocity;
}
}
if (Input.GetButtonUp ("Jump")) {
if (velocity.y > minJumpVelocity) {
velocity.y = minJumpVelocity;
}
}
velocity.y += gravity * Time.deltaTime;
controller.Move (velocity * Time.deltaTime,input);
if (controller.collisions.above || controller.collisions.below) {
velocity.y = 0;
}
}
}
Comment
specifically, jumping from the ground is at line 94-95