Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kashifkamil · Apr 22, 2016 at 03:06 PM 0
Share

specifically, jumping from the ground is at line 94-95

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

59 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Hi thanks in advance , i can't find the error in my code but my character can't stop from going up when i jump onto the platform i made which has a platform effector 2D applied to it 0 Answers

How do I set the jump distance and height to my player? 0 Answers

My game seems to be running too fast for certain tasks 0 Answers

CharacterController doesn't jump properly while moving into platform 0 Answers

How can i add a groundCheck to my script? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges