- Home /
Help with rotating my collider and ray origins
In my platformer game, I want to code it so that the player rotates to the angle of the surface hes standing on. Ive been testing out rotating my box collider 2d manually for the moment, but the points where the rays are firing from are all messed up, eventhough they point in the right direction and do actually rotate. Picture reference:
As you can see there are my rays with no rotation and then how they fire with some rotation. Im not sure why this is happening, here's my code if any of you could tell me what stupid mistake I have made:
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (BoxCollider2D))]
public class RaycastController : MonoBehaviour {
public LayerMask collisionMask;
public const float skinWidth = .015f;
public int horizontalRayCount = 4;
public int verticalRayCount = 4;
[HideInInspector]
public float horizontalRaySpacing;
[HideInInspector]
public float verticalRaySpacing;
[HideInInspector]
public BoxCollider2D collider;
public RaycastOrigins raycastOrigins;
public virtual void Awake() {
collider = GetComponent<BoxCollider2D> ();
}
public virtual void Start() {
CalculateRaySpacing ();
}
public void UpdateRaycastOrigins() {
Bounds bounds = collider.bounds;
bounds.Expand (skinWidth * -2);
raycastOrigins.bottomLeft = new Vector2 (bounds.min.x, bounds.min.y);
raycastOrigins.bottomRight = new Vector2 (bounds.max.x, bounds.min.y);
raycastOrigins.topLeft = new Vector2 (bounds.min.x, bounds.max.y);
raycastOrigins.topRight = new Vector2 (bounds.max.x, bounds.max.y);
}
public void CalculateRaySpacing() {
Bounds bounds = collider.bounds;
bounds.Expand (skinWidth * -2);
horizontalRayCount = Mathf.Clamp (horizontalRayCount, 2, int.MaxValue);
verticalRayCount = Mathf.Clamp (verticalRayCount, 2, int.MaxValue);
horizontalRaySpacing = bounds.size.y / (horizontalRayCount - 1);
verticalRaySpacing = bounds.size.x / (verticalRayCount - 1);
}
public struct RaycastOrigins {
public Vector2 topLeft, topRight;
public Vector2 bottomLeft, bottomRight;
}
}
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 ++) {
Vector3 rayOrigin = (directionX == -1)?raycastOrigins.bottomLeft:raycastOrigins.bottomRight;
rayOrigin += transform.up * (horizontalRaySpacing * i);
RaycastHit2D hit = Physics2D.Raycast(rayOrigin, transform.right * directionX, rayLength, collisionMask);
Debug.DrawRay(rayOrigin, transform.right * directionX * rayLength,Color.white);
if (hit) {
velocity.x = (hit.distance - skinWidth) * directionX;
rayLength = hit.distance;
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 ++) {
Vector3 rayOrigin = (directionY == -1)?raycastOrigins.bottomLeft:raycastOrigins.topLeft;
rayOrigin += transform.right * (verticalRaySpacing * i + velocity.x);
RaycastHit2D hit = Physics2D.Raycast(rayOrigin, transform.up * directionY, rayLength, collisionMask);
Debug.DrawRay(rayOrigin, transform.up * directionY * rayLength,Color.white);
if (hit) {
if (hit.collider.tag == "Through") {
if (directionY == 1 || hit.distance == 0) {
continue;
}
if (collisions.fallingThroughPlatform) {
continue;
}
if (playerInput.y == -1) {
collisions.fallingThroughPlatform = true;
Invoke("ResetFallingThroughPlatform",.5f);
continue;
}
}
velocity.y = (hit.distance - skinWidth) * directionY;
rayLength = hit.distance;
collisions.below = directionY == -1;
collisions.above = directionY == 1;
}
}
}
Thanks :)
I think this is to do with how the bounding box rotates differently to the collider so the bounds are different, but I dont know how to fix that....
Your answer
Follow this Question
Related Questions
Tilemap Collider wrong rotation 0 Answers
Non-rotating collider fixed to a rolling ball 1 Answer
Animal Rigidbody and Collider problem 0 Answers
SInce when?!?!? (Raycast issue) 2 Answers