• 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 /
avatar image
Question by S4breK4t · Aug 20, 2015 at 12:16 PM · platformerangleslope

Slope angle fix

Im simply trying to print out the slope angle because I will need to use later, but when Im on a slope it keeps changing between 45 (the actual angle) and 0, while im decending or next to a wall. I cant work out why this is happening, so Id really appreciate help. Here's my code.

 using UnityEngine;
 using System.Collections;
 
 public class Controller2D : RaycastController {
     
     public bool movementStopped = false;
     public bool canRotate = false;
 
     float maxClimbAngle = 50;
     float maxDescendAngle = 50;
     public float staticSlopeAngle = 0;
 
     bool rotated = false;
 
 
     public CollisionInfo collisions;
     [HideInInspector]
     public Vector2 playerInput;
 
 
     
     public override void Start() {
 
         base.Start ();
         collisions.faceDir = 1;
         
     }
 
     void Update () {
         boxCollider = GetComponent<BoxCollider2D> ();
         UpdateRaycastOrigins ();
         CalculateRaySpacing ();
 
         print (collisions.slopeAngle);
 
     }
 
     void LateUpdate () {
         float directionX = collisions.faceDir;
         if (directionX == -1) {
             //transform.RotateAround (bottomLeft, -transform.forward, collisions.slopeAngle);
         } else if (directionX == 1) {
             //transform.RotateAround (bottomRight, transform.forward, collisions.slopeAngle);
         }
         print (collisions.slopeAngle);
 
 
 
     }
 
     public void Move(Vector3 velocity, bool standingOnPlatform) {
         movementStopped = false;
         Move (velocity, Vector2.zero, standingOnPlatform);
     }
     
     public void Move(Vector3 velocity, Vector2 input, bool standingOnPlatform = false) {
         movementStopped = false;
 
         collisions.Reset ();
         collisions.velocityOld = velocity;
         playerInput = input;
 
 
         
         if (velocity.x != 0) {
             collisions.faceDir = (int)Mathf.Sign(velocity.x);
         }
 
         if (velocity.y < 0) {
             DescendSlope(ref velocity);
         }
 
         
         HorizontalCollisions (ref velocity);
 
         if (velocity.y != 0) {
             VerticalCollisions (ref velocity);
         }
 
         transform.Translate (velocity);
         
         if (standingOnPlatform) {
             collisions.below = true;
         }
     }
 
     void HorizontalCollisions(ref Vector3 velocity) {
         float directionX = Mathf.Sign (velocity.x);
         float rayLength = Mathf.Abs (velocity.x) + skinWidth;
         
         for (int i = 0; i < horizontalRayCount; i ++) {
             Vector2 rayOrigin = (directionX == -1)?bottomLeft: bottomRight;
             rayOrigin += Vector2.up * (horizontalRaySpacing * i);
             RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.right * directionX, rayLength, collisionMask);
             
             Debug.DrawRay(rayOrigin, Vector2.right * directionX * rayLength,Color.red);
             
             if (hit) {
                 
                 if (hit.distance == 0) {
                     continue;
                 }
                 
                 float slopeAngle = Vector2.Angle(hit.normal, Vector2.up);
                 
                 if (i == 0 && slopeAngle <= maxClimbAngle) {
                     if (collisions.descendingSlope) {
                         collisions.descendingSlope = false;
                         velocity = collisions.velocityOld;
                     }
                     float distanceToSlopeStart = 0;
                     if (slopeAngle != collisions.slopeAngleOld) {
                         distanceToSlopeStart = hit.distance-skinWidth;
                         velocity.x -= distanceToSlopeStart * directionX;
                     }
                     ClimbSlope(ref velocity, slopeAngle);
                     velocity.x += distanceToSlopeStart * directionX;
                 }
                 
                 if (!collisions.climbingSlope || slopeAngle > maxClimbAngle) {
                     velocity.x = (hit.distance - skinWidth) * directionX;
                     rayLength = hit.distance;
                     
                     if (collisions.climbingSlope) {
                         velocity.y = Mathf.Tan(collisions.slopeAngle * Mathf.Deg2Rad) * Mathf.Abs(velocity.x);
                     }
 
                     movementStopped = true;
                     
                     collisions.left = directionX == -1;
                     collisions.right = directionX == 1;
                 }
             }
         }
     }
     
     void VerticalCollisions(ref Vector3 velocity) {
         float directionY = Mathf.Sign (velocity.y);
         float rayLength = Mathf.Abs (velocity.y) + skinWidth;
         
         for (int i = 0; i < verticalRayCount; i ++) {
             
             Vector2 rayOrigin = (directionY == -1)?bottomLeft: topLeft;
             rayOrigin += Vector2.right * (verticalRaySpacing * i + velocity.x);
             RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up * directionY, rayLength, collisionMask);
             
             Debug.DrawRay(rayOrigin, Vector2.up * directionY * rayLength,Color.red);
             
             if (hit) {
                 
                 velocity.y = (hit.distance - skinWidth) * directionY;
                 rayLength = hit.distance;
                 
                 if (collisions.climbingSlope) {
                     velocity.x = velocity.y / Mathf.Tan(collisions.slopeAngle * Mathf.Deg2Rad) * Mathf.Sign(velocity.x);
                 }
                 
                 collisions.below = directionY == -1;
                 collisions.above = directionY == 1;
             }
         }
         
         if (collisions.climbingSlope) {
             float directionX = Mathf.Sign(velocity.x);
             rayLength = Mathf.Abs(velocity.x) + skinWidth;
             Vector3 rayOrigin = ((directionX == -1)? bottomLeft: bottomRight) + transform.up * velocity.y;
             RaycastHit2D hit = Physics2D.Raycast(rayOrigin,Vector2.right * directionX,rayLength,collisionMask);
             
             if (hit) {
                 float slopeAngle = Vector2.Angle(hit.normal,Vector2.up);
                 if (slopeAngle != collisions.slopeAngle) {
                     velocity.x = (hit.distance - skinWidth) * directionX;
                     collisions.slopeAngle = slopeAngle;
                 }
             }
         }
     }
     
     void ClimbSlope(ref Vector3 velocity, float slopeAngle) {
         float moveDistance = Mathf.Abs (velocity.x);
         float climbVelocityY = Mathf.Sin (slopeAngle * Mathf.Deg2Rad) * moveDistance;
         
         if (velocity.y <= climbVelocityY) {
             velocity.y = climbVelocityY;
             velocity.x = Mathf.Cos (slopeAngle * Mathf.Deg2Rad) * moveDistance * Mathf.Sign (velocity.x);
             collisions.below = true;
             collisions.climbingSlope = true;
             collisions.slopeAngle = slopeAngle;
         }
     }
     
     void DescendSlope(ref Vector3 velocity) {
         float directionX = Mathf.Sign (velocity.x);
         Vector2 rayOrigin = (directionX == -1) ? bottomRight : bottomLeft;
         RaycastHit2D hit = Physics2D.Raycast (rayOrigin, -Vector2.up, Mathf.Infinity, collisionMask);
         
         if (hit) {
             float slopeAngle = Vector2.Angle(hit.normal, Vector2.up);
             if (slopeAngle != 0 && slopeAngle <= maxDescendAngle) {
                 if (Mathf.Sign(hit.normal.x) == directionX) {
                     if (hit.distance - skinWidth <= Mathf.Tan(slopeAngle * Mathf.Deg2Rad) * Mathf.Abs(velocity.x)) {
                         float moveDistance = Mathf.Abs(velocity.x);
                         float descendVelocityY = Mathf.Sin (slopeAngle * Mathf.Deg2Rad) * moveDistance;
                         velocity.x = Mathf.Cos (slopeAngle * Mathf.Deg2Rad) * moveDistance * Mathf.Sign (velocity.x);
                         velocity.y -= descendVelocityY;
                         
                         collisions.slopeAngle = slopeAngle;
                         collisions.descendingSlope = true;
                         collisions.below = true;
                     }
                 }
             }
         }
     }
     
     
     
     public struct CollisionInfo {
         public bool above, below;
         public bool left, right;
         
         public bool climbingSlope;
         public bool descendingSlope;
         public float slopeAngle, slopeAngleOld;
         public Vector3 velocityOld;
         public int faceDir;
         
         public void Reset() {
             above = below = false;
             left = right = false;
             climbingSlope = false;
             descendingSlope = false;
             
             slopeAngleOld = slopeAngle;
             slopeAngle = 0;
         }
     }
 }
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

2d platformer climb slopes problem 0 Answers

Camera direction depending on targets slope (X axis) 0 Answers

2D Platform Player moving instantly from upper platform to lower, only when moving left 0 Answers

Checking slope angle the object is standing on? 1 Answer

2D 360 degress platformer example needed 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