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 Bairne · Dec 06, 2017 at 02:53 PM · raycast2d-platformerslopesphysics 2d

Handling Slopes

Hi Folks,

I'm a pretty newbie developer who's been using tutorial to expand their knowledge of game design etc.

I've recently started working with a Super Meat Boy style character controller, which is awesome! My one issue with it though is that it can't handle slopes.

I've tried a variety of suggested fixes from this forum, but haven't had any luck, and i'm afraid my inexperience is presenting a road block here.

If anyone can suggest a way to get the controller to handle slopes, that would be amazing! Thanks for all your help.

Current Controller:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour {
 
     //floats and bools
 
     public float MaxSpeed = 10;
     public float Acceleration = 35;
 
     public float JumpSpeed = 8;
 
     public float JumpDuration;
 
     public bool EnableDoubleJump = true;
 
     public bool wallHitDoubleJumpOverride = true;
 
     public bool facingRight = false;
 
     //internal checks
 
     bool canDoubleJump = true;
 
     float jmpDuration;
 
     bool jumpKeyDown = false;
 
     bool canVariableJump = false;
 
     //refs
 
     private Renderer render;
     private Rigidbody2D rigid2d;
 
     // Use this for initialization
     void Start () {
         render = GetComponent<Renderer> ();
         rigid2d = GetComponent<Rigidbody2D> ();
     }
     
     // Update is called once per frame
     void Update () {
         
         float horizontal = Input.GetAxis ("Horizontal");
         if (horizontal < -0.1f) {
             if (rigid2d.velocity.x > -this.MaxSpeed) {
                 FlipPlayer ();
                 rigid2d.AddForce (new Vector2 (-this.Acceleration, 0.0f));
             } else {
                 rigid2d.velocity = new Vector2 (-this.MaxSpeed, rigid2d.velocity.y);
             }
         }
         else if (horizontal > 0.1f) {
             if (rigid2d.velocity.x < this.MaxSpeed) {
                 
                 rigid2d.AddForce (new Vector2 (this.Acceleration, 0.0f));
             } else {
                 rigid2d.velocity = new Vector2 (this.MaxSpeed, rigid2d.velocity.y);
             }
         }
 
         bool Grounded = isGrounded ();
 
         float vertical = Input.GetAxis ("Vertical");
 
         if (Grounded) {
             
             canDoubleJump = true;
         }
         if (vertical > 0.1f) {
             if (!jumpKeyDown) { //1st jump frame
                 jumpKeyDown = true;
 
 
                 if (Grounded || (canDoubleJump && EnableDoubleJump) || wallHitDoubleJumpOverride) {
                     bool wallHit = false;
                     int wallHitDirection = 0;
 
                     bool leftWallHit = isWallOnLeft ();
                     bool rightWallHit = isWallOnRight ();
 
                     if (horizontal != 0) {
                         if (leftWallHit) {
                             wallHit = true;
                             wallHitDirection = 1;
                         } else if (rightWallHit) {
                             wallHit = true;
                             wallHitDirection = -1;
                         }
                     }
 
                     if (!wallHit) {
                         if (Grounded || (canDoubleJump && EnableDoubleJump)) {
                             rigid2d.velocity = new Vector2 (rigid2d.velocity.x, this.JumpSpeed);
                             jmpDuration = 0.0f;
                             canVariableJump = true;
                         }
                     } else {
                         rigid2d.velocity = new Vector2 (this.JumpSpeed * wallHitDirection, this.JumpSpeed);
 
                         jmpDuration = 0.0f;
                         canVariableJump = true;
                     }
                     if (!Grounded && !wallHit) {
                         canDoubleJump = false;
                     }
                 }
             }//2nd Jump Frame
                 else if (canVariableJump) {
                     
                     jmpDuration += Time.deltaTime;
                     if (jmpDuration < this.JumpDuration / 1000) {
                         rigid2d.velocity = new Vector2 (rigid2d.velocity.x, this.JumpSpeed);
                     }
                 }
             } else {
                 jumpKeyDown = false;
                 canVariableJump = false;
 
         }
         if (horizontal < 0.0f && facingRight == false) {
             FlipPlayer ();
         } else if (horizontal > 0.0f && facingRight == true) {
             FlipPlayer ();
         }
     }
     //Detects Grounding
     private bool isGrounded(){
         float lengthToSearch = 0.1f;
         float colliderThreshold = 0.001f;
 
         Vector2 linestart = new Vector2 (this.transform.position.x, this.transform.position.y - this.render.bounds.extents.y - colliderThreshold);
 
         Vector2 vectorToSearch = new Vector2 (this.transform.position.x, linestart.y - lengthToSearch);
 
         RaycastHit2D hit = Physics2D.Linecast (linestart, vectorToSearch);
 
         return hit;
     }
     //Detects Wall Hits
     private bool isWallOnLeft(){
 
         bool retVal = false;
 
         float lengthToSearch = 0.1f;
         float colliderThreshold = 0.01f;
 
         Vector2 linestart = new Vector2 (this.transform.position.x - this.render.bounds.extents.x - colliderThreshold, this.transform.position.y);
         Vector2 vectorToSearch = new Vector2 (linestart.x - lengthToSearch, this.transform.position.y);
 
         RaycastHit2D hitLeft = Physics2D.Linecast (linestart, vectorToSearch);
 
         retVal = hitLeft;
 
         if (retVal) {
             if (hitLeft.collider.GetComponent<NoSlideJump> ()) {
                 retVal = false;
             }
         }
 
         return retVal;
     }
 
     private bool isWallOnRight(){
         bool retVal = false;
 
         float lengthToSearch = 0.1f;
         float colliderThreshold = 0.01f;
 
         Vector2 linestart = new Vector2 (this.transform.position.x + this.render.bounds.extents.x + colliderThreshold, this.transform.position.y);
         Vector2 vectorToSearch = new Vector2 (linestart.x + lengthToSearch, this.transform.position.y);
 
         RaycastHit2D hitRight = Physics2D.Linecast (linestart, vectorToSearch);
 
         retVal = hitRight;
 
         if (retVal) {
             if (hitRight.collider.GetComponent<NoSlideJump> ()) {
                 retVal = false;
             }
         }
 
         return retVal;
     
 
     }
     void FlipPlayer(){
         facingRight = !facingRight;
         Vector2 localScale = gameObject.transform.localScale;
         localScale.x *= -1;
         transform.localScale = localScale;
     }
 
 }
 
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

149 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 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 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

How to detect slope angle in 2D 1 Answer

How to Raycast 2D Diagonally instead of Horizontal on a Platformer game. 0 Answers

Change X rotation on slope - Only way is Raycast? 0 Answers

Issue with slopes in unity 2D, can't find solution. 1 Answer

Inconsistent Jump Heights 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