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 ThomasSoto · Aug 11, 2017 at 06:47 AM · raycastcrashplatformerdictionarystackoverflow

I got StackOverflowException on 2D Platform scripts

Hi, I'm pretty new to coding and I've been using some base scripts from some youtubers (Specially Sebastian Lague and Brackeys, BIG THANKS!) and I've been enhancing depending on my game needs. I'm working with this code for various reasons but currently, I'm making a box (with tag "Push") that can be moved around by commands sent by another script to the Move function. Also, this script calculates movement for objects on top of it so they move along with it as in an elevator or pushing a tower of boxes.

The issue is that for some reason a box is on top of another and both have the tag "Push" and this same script, then I get StackOverFlowException. If the player stands on top of the box while it's being moved by elevator then all is ok. I have read what a StackOverFlowException is but I'm not quite sure why it's happening in this case. I really appreciate any help!

public class DynamicController2D : RaycastController {

 float maxClimbAngle = 1;
 float maxDescendAngle = 1;

 public LayerMask playerMask;
 public LayerMask passengerMask;

 public CollisionInfo collisions;
 [HideInInspector]
 public Vector2 playerInput;

 List<PassengerMovement> passengerMovement;
 Dictionary<Transform,Controller2D> passengerDictionary = new Dictionary<Transform, Controller2D>();
 Dictionary<Transform,DynamicController2D> objectPassengerDictionary = new Dictionary<Transform, DynamicController2D> ();

 public override void Start() {
     base.Start ();
     collisions.faceDir = 1;
 }

 public void Move(Vector3 velocity, bool standingOnPlatform = false) {

     UpdateRaycastOrigins ();
     collisions.Reset ();
     collisions.velocityOld = velocity;

     CalculatePassengerMovement (velocity);

     if (velocity.x != 0) {
         collisions.faceDir = (int)Mathf.Sign(velocity.x);
     }

     HorizontalCollisions (ref velocity);
     VerticalCollisions (ref velocity);

     MovePassengers (true);
     transform.Translate (velocity);
     MovePassengers (false);

     if (standingOnPlatform) {
         collisions.below = true;
     }
         
 }
     
 void HorizontalCollisions(ref Vector3 velocity) {

     float directionX = collisions.faceDir;

     float rayLength = Mathf.Abs (velocity.x) + skinWidth;

     if (Mathf.Abs(velocity.x) < skinWidth) {
         rayLength = 2*skinWidth;
     }

     for (int i = 0; i < horizontalRayCount; i ++) {
         Vector2 rayOrigin = (directionX == -1)?raycastOrigins.bottomLeft:raycastOrigins.bottomRight;
         Vector2 rayOriginL = raycastOrigins.bottomLeft;
         Vector2 rayOriginR = raycastOrigins.bottomRight;
         rayOriginL += Vector2.up * (horizontalRaySpacing * i);
         rayOriginR += Vector2.up * (horizontalRaySpacing * i);
         RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.right * directionX, rayLength, horizontalCollisionMask);
         RaycastHit2D hitPlayerL = Physics2D.Raycast(rayOriginL, Vector2.left, rayLength, playerMask);
         RaycastHit2D hitPlayerR = Physics2D.Raycast(rayOriginR, Vector2.right, rayLength, playerMask);

         Debug.DrawRay(rayOrigin, Vector2.right * directionX * rayLength,Color.red);
         Debug.DrawRay(rayOriginL, Vector2.left * rayLength,Color.blue);
         Debug.DrawRay(rayOriginR, Vector2.right * rayLength,Color.blue);


         if (hit) {

             if (hit.distance == 0) {
                 continue;
             }


             float slopeAngle = Vector2.Angle(hit.normal, Vector2.up);


             if (i == 0 && slopeAngle <= maxClimbAngle) {
                 velocity = collisions.velocityOld;
             }

             velocity.x = (hit.distance - skinWidth) * directionX;
             rayLength = hit.distance;

             collisions.left = directionX == -1;
             collisions.right = directionX == 1;
         }

         if (hitPlayerL) {
             if (hitPlayerL.distance == 0) {
                 continue;
             }
             collisions.leftPlayer = true;
         }

         if (hitPlayerR) {
             if (hitPlayerR.distance == 0) {
                 continue;
             }
             collisions.rightPlayer = true;
         }
     }

 }

 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)?raycastOrigins.bottomLeft:raycastOrigins.topLeft;
         rayOrigin += Vector2.right * (verticalRaySpacing * i + velocity.x);
         RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up * directionY, rayLength, verticalCollisionMask);

         Debug.DrawRay(rayOrigin, Vector2.up * directionY * rayLength,Color.red);

         if (hit) {
             velocity.y = (hit.distance - skinWidth) * directionY;
             rayLength = hit.distance;

             collisions.below = directionY == -1;
             collisions.above = directionY == 1;
         }
     }


 }

 public struct CollisionInfo {
     public bool above, below;
     public bool left, right;
     public bool abovePlayer, belowPlayer;
     public bool leftPlayer, rightPlayer;

     public Vector3 velocityOld;
     public int faceDir;

     public void Reset() {
         above = below = false;
         left = right = false;
         leftPlayer = rightPlayer = false;
         abovePlayer = belowPlayer = false;
     }
 }


 void MovePassengers(bool beforeMovePlatform) {
     foreach (PassengerMovement passenger in passengerMovement) {

         if (!passengerDictionary.ContainsKey(passenger.transform) && (passenger.transform.tag == ("Player") || passenger.transform.tag == ("Partner"))
             || !objectPassengerDictionary.ContainsKey(passenger.transform) && (passenger.transform.tag == ("Push"))) {

             if (passenger.transform.tag == ("Player") || passenger.transform.tag == ("Partner")) {
                 passengerDictionary.Add (passenger.transform, passenger.transform.GetComponent<Controller2D> ());
             } else if (passenger.transform.tag == ("Push")) {
                 objectPassengerDictionary.Add(passenger.transform,passenger.transform.GetComponent<DynamicController2D>());
             }
         }

         if (passenger.moveBeforePlatform == beforeMovePlatform) {
             if (passenger.transform.tag == ("Player") || passenger.transform.tag == ("Partner")) {
                 passengerDictionary [passenger.transform].Move (passenger.velocity, passenger.standingOnPlatform);
             } else if (passenger.transform.tag == ("Push")) {
                 objectPassengerDictionary[passenger.transform].Move(passenger.velocity, passenger.standingOnPlatform);
             }
         }

     }
 }

 void CalculatePassengerMovement(Vector3 velocity) {
     HashSet<Transform> movedPassengers = new HashSet<Transform> ();
     passengerMovement = new List<PassengerMovement> ();

     float directionX = Mathf.Sign (velocity.x);
     float directionY = Mathf.Sign (velocity.y);

     // Vertically moving platform
     if (velocity.y != 0) {
         float rayLength = Mathf.Abs (velocity.y) + skinWidth;

         for (int i = 0; i < verticalRayCount; i ++) {
             Vector2 rayOrigin = (directionY == -1)?raycastOrigins.bottomLeft:raycastOrigins.topLeft;
             rayOrigin += Vector2.right * (verticalRaySpacing * i);
             RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up * directionY, rayLength, passengerMask);

             if (hit && hit.distance != 0) {

                 if (!movedPassengers.Contains(hit.transform)) {
                     movedPassengers.Add(hit.transform);
                     float pushX = (directionY == 1)?velocity.x:0;
                     float pushY = velocity.y - (hit.distance - skinWidth) * directionY;

                     passengerMovement.Add(new PassengerMovement(hit.transform,new Vector3(pushX,pushY), directionY == 1, true));
                 }
             }
         }
     }

     // Horizontally moving platform
     if (velocity.x != 0) {
         float rayLength = Mathf.Abs (velocity.x) + skinWidth;

         for (int i = 0; i < horizontalRayCount; i ++) {
             Vector2 rayOrigin = (directionX == -1)?raycastOrigins.bottomLeft:raycastOrigins.bottomRight;
             rayOrigin += Vector2.up * (horizontalRaySpacing * i);
             RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.right * directionX, rayLength, passengerMask);

             if (hit && hit.distance != 0) {
                 if (!movedPassengers.Contains(hit.transform)) {
                     movedPassengers.Add(hit.transform);
                     float pushX = velocity.x - (hit.distance - skinWidth) * directionX;
                     float pushY = -skinWidth;

                     passengerMovement.Add(new PassengerMovement(hit.transform,new Vector3(pushX,pushY), false, true));
                 }
             }
         }
     }

     // Passenger on top of a horizontally or downward moving platform
     if (directionY == -1 || velocity.y == 0 && velocity.x != 0) {
         float rayLength = skinWidth * 2;

         for (int i = 0; i < verticalRayCount; i ++) {
             Vector2 rayOrigin = raycastOrigins.topLeft + Vector2.right * (verticalRaySpacing * i);
             RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up, rayLength, passengerMask);

             if (hit && hit.distance != 0) {
                 if (!movedPassengers.Contains(hit.transform)) {
                     movedPassengers.Add(hit.transform);
                     float pushX = velocity.x;
                     float pushY = velocity.y;

                     passengerMovement.Add(new PassengerMovement(hit.transform,new Vector3(pushX,pushY), true, false));
                 }
             }
         }
     }
 }

 struct PassengerMovement {
     public Transform transform;
     public Vector3 velocity;
     public bool standingOnPlatform;
     public bool moveBeforePlatform;

     public PassengerMovement(Transform _transform, Vector3 _velocity, bool _standingOnPlatform, bool _moveBeforePlatform) {
         transform = _transform;
         velocity = _velocity;
         standingOnPlatform = _standingOnPlatform;
         moveBeforePlatform = _moveBeforePlatform;
     }
 }


}

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

142 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

Related Questions

2d platformer knockback without addforce 1 Answer

Help with Layer mask to not detect player hitbox 0 Answers

Raycasting doesn't work after 'fatal error' 1 Answer

Raycast Reflection 2D 1 Answer

Unity crashes unexpectedly when raycast hits an object with an enemy tag,Unity crashes unexpectedly when raycast hits an object with enemy tag 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