• 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
Question by Nomppie · Jan 19, 2018 at 08:03 PM · 2d2d game2d-platformer2d-physics

How to Handle Slopes in 2D Platformers?

I haven't found a ton of useful information on this so far, so I'm just going to put this question out there. What's the best way to ensure that

1) My character's local y axis is always perpendicular to the ground underneath them, and 2) My character won't be pushed downwards by gravity while standing on a downward angled slope

Any kind of help on this is greatly appreciated

Comment

People who like this

0 Show 0
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

1 Reply

  • Sort: 
avatar image

Answer by Nomppie · Jan 22, 2018 at 01:57 AM

I found an answer deep in the bowels of google that gives me the slope information i need, and it looks like this:

 RaycastHit2D[] hits = new RaycastHit2D[2];
         int h = Physics2D.RaycastNonAlloc(transform.position, -Vector2.up, hits,3.5f,myMask); //cast downwards
         if (h > 1) { //if we hit something do stuff
             
             angle = Mathf.Abs(Mathf.Atan2(hits[1].normal.y, hits[1].normal.x)*Mathf.Rad2Deg); //get angle
 
             Debug.Log (angle);
 
         }

This gets you the angle of the slope against the player's up vector. So if I want my character to rotate to be perpendicular to the slope, I just have to make my character's rotation plus the angle equal 90. I'm going to paste the code I have in just a sec, but for some reason, if the desired rotation is negative, it just breaks my code and I really don't understand why. My character just keeps spinning clockwise and it makes no sense

 if (myGroundCheck.GetComponent<GroundCheckScript> ().isGrounded) {
             targetRotation = angle - 90;
         } else if (!myGroundCheck.GetComponent<GroundCheckScript> ().isGrounded) {
             targetRotation = 0;
         }
 
 
 
         if (transform.rotation.eulerAngles.z < targetRotation) {
             //if our rotation is less than the target rotation, add up to it
             Vector3 tempRot = transform.rotation.eulerAngles;
             tempRot.z += correctionRate * Time.deltaTime;
             tempRot.z = Mathf.Clamp (tempRot.z, -slopeLimit, targetRotation);
             transform.rotation = Quaternion.Euler(tempRot);
 
         } else if (transform.rotation.eulerAngles.z > targetRotation) {
             //if our rotation is more than the target rotation, subract down to it
             Vector3 tempRot = transform.rotation.eulerAngles;
             tempRot.z -= correctionRate * Time.deltaTime;
             tempRot.z = Mathf.Clamp (tempRot.z, targetRotation, slopeLimit);
             transform.rotation = Quaternion.Euler(tempRot);
         }


Comment

People who like this

0 Show 2 · Share
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 Nomppie · Jan 22, 2018 at 02:31 AM 0
Share

This is extra weird because I can go from a positive rotation to zero no problem. I'm extra confused why negatives don't work here

avatar image Nomppie · Jan 22, 2018 at 07:41 PM 0
Share

The problem isn't with the code, it's with how my ground checking system works. I'll just have to change that up and do some fine tuning.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

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

Player controlled platforms 0 Answers

[2D] Rigid body snaps through floor and back 1 Answer

2d platformer physics 0 Answers

Collision Not regestering 1 Answer

Problem with my dash ability, 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