starting slope stuff

master
Jordan Orelli 5 years ago
parent 243f054d93
commit a3fc4638f7

@ -9,7 +9,6 @@ public class MoveController : MonoBehaviour
public const float skinWidth = 0.015f; public const float skinWidth = 0.015f;
public int horizontalRayCount = 4; public int horizontalRayCount = 4;
public int verticalRayCount = 4; public int verticalRayCount = 4;
public int forwardRayCount = 4;
public CollisionInfo collisions; public CollisionInfo collisions;
public float maxClimbAngle = 80f; public float maxClimbAngle = 80f;
@ -17,7 +16,6 @@ public class MoveController : MonoBehaviour
private RaycastOrigins raycastOrigins; private RaycastOrigins raycastOrigins;
private float horizontalRaySpacing; private float horizontalRaySpacing;
private float verticalRaySpacing; private float verticalRaySpacing;
private float forwardRaySpacing;
void Start() { void Start() {
collider = GetComponent<BoxCollider>(); collider = GetComponent<BoxCollider>();
@ -28,54 +26,51 @@ public class MoveController : MonoBehaviour
UpdateRaycastOrigins(); UpdateRaycastOrigins();
collisions.Reset(); collisions.Reset();
HorizontalCollisions(ref velocity); if (velocity.x != 0) {
VerticalCollisions(ref velocity); HorizontalCollisions(ref velocity);
// if (velocity.x != 0) { }
// } if (velocity.y != 0) {
// if (velocity.y != 0) { VerticalCollisions(ref velocity);
// } }
transform.Translate(velocity); transform.Translate(velocity);
} }
private void HorizontalCollisions(ref Vector3 velocity) { private void HorizontalCollisions(ref Vector3 velocity) {
// https://www.youtube.com/watch?v=cwcC2tIKObU&t=331s
float directionX = Mathf.Sign(velocity.x); // -1 for left, 1 for right float directionX = Mathf.Sign(velocity.x); // -1 for left, 1 for right
float rayLength = Mathf.Abs(velocity.x) + skinWidth; float rayLength = Mathf.Abs(velocity.x) + skinWidth;
for (int i = 0; i < horizontalRayCount; i++) { for (int i = 0; i < horizontalRayCount; i++) {
for (int j = 0; j < forwardRayCount; j++) { Vector3 rayOrigin = (directionX == -1) ? raycastOrigins.bottomLeft : raycastOrigins.bottomRight;
Vector3 rayOrigin = (directionX == -1) ? raycastOrigins.bottomFrontLeft : raycastOrigins.bottomFrontRight; rayOrigin += Vector3.up * (horizontalRaySpacing * i);
rayOrigin += Vector3.up * (horizontalRaySpacing * i);
rayOrigin += Vector3.forward * (forwardRaySpacing * j);
RaycastHit[] hits = Physics.RaycastAll(rayOrigin, Vector3.right * directionX, rayLength, collisionMask);
if (hits.Length == 0) {
Debug.DrawRay(rayOrigin + velocity, Vector3.right * directionX * rayLength, Color.magenta);
continue;
}
RaycastHit hit = hits[0]; RaycastHit[] hits = Physics.RaycastAll(rayOrigin, Vector3.right * directionX, rayLength, collisionMask);
for (int h = 1; h < hits.Length; h++) { if (hits.Length == 0) {
if (hits[h].distance < hit.distance) { Debug.DrawRay(rayOrigin + velocity, Vector3.right * directionX * rayLength, Color.magenta);
hit = hits[h]; continue;
} }
}
float slopeAngle = Vector3.Angle(hit.normal, Vector3.up); RaycastHit hit = hits[0];
if (i == 0 && j == 0) { for (int h = 1; h < hits.Length; h++) {
Debug.LogFormat("Slope angle: {0}", slopeAngle); if (hits[h].distance < hit.distance) {
hit = hits[h];
} }
}
velocity.x = (hit.distance - skinWidth) * directionX; float slopeAngle = Vector3.Angle(hit.normal, Vector3.up);
// Debug.LogFormat("with RayLength {0} MinHitDist {1} setting velocity.y to {2}", rayLength, hit.distance, velocity.y); if (i == 0) {
rayLength = hit.distance; Debug.LogFormat("Slope angle: {0}", slopeAngle);
Debug.DrawRay(rayOrigin + velocity, Vector3.right * directionX * rayLength, Color.red); ClimbSlope(ref velocity, slopeAngle);
}
if (directionX == -1) { velocity.x = (hit.distance - skinWidth) * directionX;
collisions.left = true; // Debug.LogFormat("with RayLength {0} MinHitDist {1} setting velocity.y to {2}", rayLength, hit.distance, velocity.y);
} else { rayLength = hit.distance;
collisions.right = true; Debug.DrawRay(rayOrigin + velocity, Vector3.right * directionX * rayLength, Color.red);
}
if (directionX == -1) {
collisions.left = true;
} else {
collisions.right = true;
} }
} }
} }
@ -85,49 +80,48 @@ public class MoveController : MonoBehaviour
float rayLength = Mathf.Abs(velocity.y) + skinWidth; float rayLength = Mathf.Abs(velocity.y) + skinWidth;
for (int i = 0; i < verticalRayCount; i++) { for (int i = 0; i < verticalRayCount; i++) {
for (int j = 0; j < forwardRayCount; j++) { Vector3 rayOrigin = (directionY == -1) ? raycastOrigins.bottomLeft : raycastOrigins.topLeft;
Vector3 rayOrigin = (directionY == -1) ? raycastOrigins.bottomFrontLeft : raycastOrigins.topFrontLeft; rayOrigin += Vector3.right * (verticalRaySpacing * i);
rayOrigin += Vector3.right * (verticalRaySpacing * i); RaycastHit[] hits = Physics.RaycastAll(rayOrigin, Vector3.up * directionY, rayLength, collisionMask);
rayOrigin += Vector3.forward * (forwardRaySpacing * j); if (hits.Length == 0) {
RaycastHit[] hits = Physics.RaycastAll(rayOrigin, Vector3.up * directionY, rayLength, collisionMask); Debug.DrawRay(rayOrigin + velocity, Vector3.up * directionY * rayLength, Color.green);
if (hits.Length == 0) { continue;
Debug.DrawRay(rayOrigin + velocity, Vector3.up * directionY * rayLength * 10, Color.green); }
continue; RaycastHit hit = hits[0];
} for (int h = 1; h < hits.Length; h++) {
RaycastHit hit = hits[0]; if (hits[h].distance < hit.distance) {
for (int h = 1; h < hits.Length; h++) { hit = hits[h];
if (hits[h].distance < hit.distance) {
hit = hits[h];
}
} }
}
velocity.y = (hit.distance - skinWidth) * directionY; velocity.y = (hit.distance - skinWidth) * directionY;
// Debug.LogFormat("with RayLength {0} MinHitDist {1} setting velocity.y to {2}", rayLength, hit.distance, velocity.y); // Debug.LogFormat("with RayLength {0} MinHitDist {1} setting velocity.y to {2}", rayLength, hit.distance, velocity.y);
rayLength = hit.distance; rayLength = hit.distance;
Debug.DrawRay(rayOrigin + velocity, Vector3.up * directionY * rayLength, Color.red); Debug.DrawRay(rayOrigin + velocity, Vector3.up * directionY * rayLength, Color.red);
if (directionY == -1) { if (directionY == -1) {
collisions.below = true; collisions.below = true;
} else { } else {
collisions.above = true; collisions.above = true;
}
} }
} }
} }
private void ClimbSlope(ref Vector3 velocity, float slopeAngle) {
float dist = Mathf.Abs(velocity.x);
velocity.y = Mathf.Sin(slopeAngle * Mathf.Deg2Rad) * dist;
velocity.x = Mathf.Cos(slopeAngle * Mathf.Deg2Rad) * dist * Mathf.Sign(velocity.x);
}
void UpdateRaycastOrigins() { void UpdateRaycastOrigins() {
Bounds bounds = collider.bounds; Bounds bounds = collider.bounds;
float depth = (bounds.max.z + bounds.min.z) * 0.5f;
bounds.Expand(skinWidth * -2); bounds.Expand(skinWidth * -2);
raycastOrigins.bottomFrontLeft = new Vector3(bounds.min.x, bounds.min.y, bounds.min.z); // 0 0 0 raycastOrigins.bottomLeft = new Vector3(bounds.min.x, bounds.min.y, depth);
raycastOrigins.bottomBackLeft = new Vector3(bounds.min.x, bounds.min.y, bounds.max.z); // 0 0 1 raycastOrigins.topLeft = new Vector3(bounds.min.x, bounds.max.y, depth);
raycastOrigins.topFrontLeft = new Vector3(bounds.min.x, bounds.max.y, bounds.min.z); // 0 1 0 raycastOrigins.bottomRight = new Vector3(bounds.max.x, bounds.min.y, depth);
raycastOrigins.topBackLeft = new Vector3(bounds.min.x, bounds.max.y, bounds.max.z); // 0 1 1 raycastOrigins.topRight = new Vector3(bounds.max.x, bounds.max.y, depth);
raycastOrigins.bottomFrontRight = new Vector3(bounds.max.x, bounds.min.y, bounds.min.z); // 1 0 0
raycastOrigins.bottomBackRight = new Vector3(bounds.max.x, bounds.min.y, bounds.max.z); // 1 0 1
raycastOrigins.topFrontRight = new Vector3(bounds.max.x, bounds.max.y, bounds.min.z); // 1 1 0
raycastOrigins.topBackRight = new Vector3(bounds.max.x, bounds.max.y, bounds.max.z); // 1 1 1
} }
void CalculateRaySpacing() { void CalculateRaySpacing() {
@ -136,24 +130,18 @@ public class MoveController : MonoBehaviour
if (horizontalRayCount < 2) { horizontalRayCount = 2; } if (horizontalRayCount < 2) { horizontalRayCount = 2; }
if (verticalRayCount < 2) { verticalRayCount = 2; } if (verticalRayCount < 2) { verticalRayCount = 2; }
if (forwardRayCount < 2) { forwardRayCount = 2; }
horizontalRaySpacing = bounds.size.y / (horizontalRayCount - 1); horizontalRaySpacing = bounds.size.y / (horizontalRayCount - 1);
verticalRaySpacing = bounds.size.x / (verticalRayCount - 1); verticalRaySpacing = bounds.size.x / (verticalRayCount - 1);
forwardRaySpacing = bounds.size.z / (forwardRayCount - 1);
} }
public bool Grounded() { return collisions.below; } public bool Grounded() { return collisions.below; }
struct RaycastOrigins { struct RaycastOrigins {
public Vector3 topBackLeft; public Vector3 topLeft;
public Vector3 topBackRight; public Vector3 topRight;
public Vector3 topFrontLeft; public Vector3 bottomLeft;
public Vector3 topFrontRight; public Vector3 bottomRight;
public Vector3 bottomBackLeft;
public Vector3 bottomBackRight;
public Vector3 bottomFrontLeft;
public Vector3 bottomFrontRight;
} }
public struct CollisionInfo { public struct CollisionInfo {

Loading…
Cancel
Save