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 20, 2016 at 08:40 PM · raycastjumping2d platformerledge

How do I add ground tolerance and ledge grabbing to Sebastian Lague's Unity 5 2D Controller?

Sebastian Lague's 2D controller uses raycasting for all movements instead of rigidbody movement. I have used other controllers but his one makes the game very fluid and responsive. He does not continue the series anymore. Link to Video Playlist Link to Source Code

How do I add the following two features to the controller?

1) Ledge grabbing so that when the Player collides just below a tall obstacle, he can stick to that position. then when a certain button is pressed, the player can move onto the top of the obstacle.

2) Ground tolerance for jumping. if the player calls for a jump a few frames just before the character touches the ground or if the player wants to jump when the character reaches the end of a platform, he reacts just a few frames too late while the character just start to fall. In both cases i want the game to be a bit forgiving when checking if the player is grounded when jumping

 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=0;
 
     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
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

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

67 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Jump Using Raycast. 0 Answers

Why this don't work in unity 2020.3.1f1, but it´s working in 2019.2.14f1? 0 Answers

Problem with double jump and characther movement 0 Answers

Enemy is shooting the opposite direction of player 1 Answer

Still constant jumping even with raycasting. 0 Answers


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