From 5a2314ea8c1ed947d763acb7c33655a620ab8c13 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Tue, 28 Jan 2020 12:40:39 -0600 Subject: [PATCH] slope climbing tweaks --- Assets/Scenes/SampleScene.unity | 186 ++++++++++++++++++++++++++++++- Assets/Scripts/MoveController.cs | 65 ++++++++--- 2 files changed, 233 insertions(+), 18 deletions(-) diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index f0b1f55..20f5e0e 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -181,6 +181,97 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0} +--- !u!1 &185101268 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 185101272} + - component: {fileID: 185101271} + - component: {fileID: 185101270} + - component: {fileID: 185101269} + m_Layer: 11 + m_Name: Cube (3) + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!65 &185101269 +BoxCollider: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 185101268} + m_Material: {fileID: 0} + m_IsTrigger: 0 + m_Enabled: 1 + serializedVersion: 2 + m_Size: {x: 1, y: 1, z: 1} + m_Center: {x: 0, y: 0, z: 0} +--- !u!23 &185101270 +MeshRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 185101268} + m_Enabled: 1 + m_CastShadows: 1 + m_ReceiveShadows: 1 + m_DynamicOccludee: 1 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_RenderingLayerMask: 1 + m_RendererPriority: 0 + m_Materials: + - {fileID: 2100000, guid: 25b52a028a50a694e898d82345ba62dc, type: 2} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 0 + m_SelectedEditorRenderState: 3 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: 0 + m_SortingLayer: 0 + m_SortingOrder: 0 +--- !u!33 &185101271 +MeshFilter: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 185101268} + m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0} +--- !u!4 &185101272 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 185101268} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 5.51, y: 2.63, z: 0} + m_LocalScale: {x: 0.25, y: 0.25, z: 0.25} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 6 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &240621025 GameObject: m_ObjectHideFlags: 0 @@ -266,12 +357,103 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 240621025} m_LocalRotation: {x: 0, y: 0, z: 0.27563733, w: 0.96126175} - m_LocalPosition: {x: 1.55, y: 0.625, z: 0} - m_LocalScale: {x: 2, y: 0.25, z: 0.25} + m_LocalPosition: {x: 7.41, y: 3.59, z: 0} + m_LocalScale: {x: 12.63, y: 0.25, z: 0.25} m_Children: [] m_Father: {fileID: 0} m_RootOrder: 5 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 32} +--- !u!1 &601208616 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 601208620} + - component: {fileID: 601208619} + - component: {fileID: 601208618} + - component: {fileID: 601208617} + m_Layer: 11 + m_Name: Cube (4) + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!65 &601208617 +BoxCollider: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 601208616} + m_Material: {fileID: 0} + m_IsTrigger: 0 + m_Enabled: 1 + serializedVersion: 2 + m_Size: {x: 1, y: 1, z: 1} + m_Center: {x: 0, y: 0, z: 0} +--- !u!23 &601208618 +MeshRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 601208616} + m_Enabled: 1 + m_CastShadows: 1 + m_ReceiveShadows: 1 + m_DynamicOccludee: 1 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_RenderingLayerMask: 1 + m_RendererPriority: 0 + m_Materials: + - {fileID: 2100000, guid: 25b52a028a50a694e898d82345ba62dc, type: 2} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 0 + m_SelectedEditorRenderState: 3 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: 0 + m_SortingLayer: 0 + m_SortingOrder: 0 +--- !u!33 &601208619 +MeshFilter: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 601208616} + m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0} +--- !u!4 &601208620 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 601208616} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 2.147, y: 1.876, z: 0} + m_LocalScale: {x: 5, y: 0.25, z: 0.25} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 7 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!4 &1063375671 stripped Transform: m_CorrespondingSourceObject: {fileID: 6383496741192187647, guid: cd445451907d7274daea79c67e13a202, diff --git a/Assets/Scripts/MoveController.cs b/Assets/Scripts/MoveController.cs index 8f28575..4c45b91 100644 --- a/Assets/Scripts/MoveController.cs +++ b/Assets/Scripts/MoveController.cs @@ -10,7 +10,7 @@ public class MoveController : MonoBehaviour public int horizontalRayCount = 4; public int verticalRayCount = 4; public CollisionInfo collisions; - public float maxClimbAngle = 80f; + public float maxClimbAngle = 60f; new private BoxCollider collider; private RaycastOrigins raycastOrigins; @@ -45,7 +45,7 @@ public class MoveController : MonoBehaviour RaycastHit[] hits = Physics.RaycastAll(rayOrigin, Vector3.right * directionX, rayLength, collisionMask); if (hits.Length == 0) { - Debug.DrawRay(rayOrigin + velocity, Vector3.right * directionX * rayLength, Color.magenta); + Debug.DrawRay(rayOrigin + velocity, Vector3.right * directionX * rayLength, Color.green); continue; } @@ -57,20 +57,32 @@ public class MoveController : MonoBehaviour } float slopeAngle = Vector3.Angle(hit.normal, Vector3.up); - if (i == 0) { - Debug.LogFormat("Slope angle: {0}", slopeAngle); + if (i == 0 && slopeAngle <= maxClimbAngle) { + float distanceToSlopeStart = 0f; + if (slopeAngle!= collisions.slopeAngleOld) { + distanceToSlopeStart = hit.distance - skinWidth; + velocity.x -= distanceToSlopeStart * directionX; + } ClimbSlope(ref velocity, slopeAngle); + velocity.x += distanceToSlopeStart * directionX; + Debug.DrawRay(rayOrigin + velocity, Vector3.right * directionX * rayLength, Color.magenta); } - velocity.x = (hit.distance - skinWidth) * directionX; - // Debug.LogFormat("with RayLength {0} MinHitDist {1} setting velocity.y to {2}", rayLength, hit.distance, velocity.y); - rayLength = hit.distance; - Debug.DrawRay(rayOrigin + velocity, Vector3.right * directionX * rayLength, Color.red); + if (!collisions.climbingSlope || slopeAngle > maxClimbAngle) { + velocity.x = (hit.distance - skinWidth) * directionX; + rayLength = hit.distance; - if (directionX == -1) { - collisions.left = true; - } else { - collisions.right = true; + if (collisions.climbingSlope) { + velocity.y = Mathf.Tan(collisions.slopeAngle * Mathf.Deg2Rad) * Mathf.Abs(velocity.x); + } + + Debug.DrawRay(rayOrigin + velocity, Vector3.right * directionX * rayLength, Color.red); + + if (directionX == -1) { + collisions.left = true; + } else { + collisions.right = true; + } } } } @@ -97,6 +109,10 @@ public class MoveController : MonoBehaviour velocity.y = (hit.distance - skinWidth) * directionY; // Debug.LogFormat("with RayLength {0} MinHitDist {1} setting velocity.y to {2}", rayLength, hit.distance, velocity.y); rayLength = hit.distance; + + if (collisions.climbingSlope) { + velocity.x = velocity.y / Mathf.Tan(collisions.slopeAngle * Mathf.Deg2Rad) * Mathf.Sign(velocity.x); + } Debug.DrawRay(rayOrigin + velocity, Vector3.up * directionY * rayLength, Color.red); if (directionY == -1) { @@ -109,8 +125,14 @@ public class MoveController : MonoBehaviour 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); + float climbY = Mathf.Sin(slopeAngle * Mathf.Deg2Rad) * dist; + if (velocity.y <= climbY) { + velocity.y = climbY; + velocity.x = Mathf.Cos(slopeAngle * Mathf.Deg2Rad) * dist * Mathf.Sign(velocity.x); + collisions.below = true; + collisions.climbingSlope = true; + collisions.slopeAngle = slopeAngle; + } } void UpdateRaycastOrigins() { @@ -149,7 +171,18 @@ public class MoveController : MonoBehaviour public bool below; public bool left; public bool right; - - public void Reset() { above = below = left = right = false; } + public bool climbingSlope; + public float slopeAngle; + public float slopeAngleOld; + + public void Reset() { + above = false; + below = false; + left = false; + right = false; + climbingSlope = false; + slopeAngleOld = slopeAngle; + slopeAngle = 0f; + } } }