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;
}
}
}
Your answer
Follow this Question
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