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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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
Add comment · 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.

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

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

What is the Easiest Way to Check if Two Time Periods are Overlapping 0 Answers

Collision Not regestering 1 Answer

Problem with my dash ability, 1 Answer

Player Not Moving With Platform 2D 1 Answer

Having trouble with 2D Jumping 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