From 97b713b9fae7283441880fd6062b55fcd646a243 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sun, 26 Jan 2020 20:06:21 -0600 Subject: [PATCH] grounded play no longer speeds up downwards --- Assets/Prefabs/Player.prefab | 5 ++ Assets/Scenes/SampleScene.unity | 123 +++++++++++++++++++++----- Assets/Scripts/MoveController.cs | 71 +++++++++++++-- Assets/Scripts/PlayerController.cs | 15 +++- ProjectSettings/DynamicsManager.asset | 63 ++++++------- 5 files changed, 216 insertions(+), 61 deletions(-) diff --git a/Assets/Prefabs/Player.prefab b/Assets/Prefabs/Player.prefab index ac1e46f..c480e1b 100644 --- a/Assets/Prefabs/Player.prefab +++ b/Assets/Prefabs/Player.prefab @@ -59,6 +59,9 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 721dfc7e43d81b644a34024174f78c4e, type: 3} m_Name: m_EditorClassIdentifier: + collisionMask: + serializedVersion: 2 + m_Bits: 2048 horizontalRayCount: 4 verticalRayCount: 4 --- !u!114 &5588890751565926871 @@ -73,7 +76,9 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 7e392b35ca7959444b61a3e29a242f2b, type: 3} m_Name: m_EditorClassIdentifier: + moveSpeed: 6 moveController: {fileID: 0} + velocity: {x: 0, y: 0, z: 0} --- !u!1 &6383496741659177281 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 067a6e9..5084589 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -272,12 +272,103 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1166482433} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: -0.5, z: 0} + m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 5, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} m_RootOrder: 3 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &1906867256 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1906867260} + - component: {fileID: 1906867259} + - component: {fileID: 1906867258} + - component: {fileID: 1906867257} + m_Layer: 11 + m_Name: Cube (1) + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!65 &1906867257 +BoxCollider: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1906867256} + 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 &1906867258 +MeshRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1906867256} + 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 &1906867259 +MeshFilter: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1906867256} + m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0} +--- !u!4 &1906867260 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1906867256} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 1.12, y: 0.625, z: 0} + m_LocalScale: {x: 0.25, y: 0.25, z: 0.25} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 4 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1001 &6383496741781373543 PrefabInstance: m_ObjectHideFlags: 0 @@ -298,7 +389,7 @@ PrefabInstance: - target: {fileID: 6383496741192187647, guid: cd445451907d7274daea79c67e13a202, type: 3} propertyPath: m_LocalPosition.y - value: 1.11 + value: 10 objectReference: {fileID: 0} - target: {fileID: 6383496741192187647, guid: cd445451907d7274daea79c67e13a202, type: 3} @@ -345,32 +436,18 @@ PrefabInstance: propertyPath: m_LocalEulerAnglesHint.z value: 0 objectReference: {fileID: 0} - - target: {fileID: 7623484707498698197, guid: cd445451907d7274daea79c67e13a202, - type: 3} - propertyPath: moveSpeed - value: 10 - objectReference: {fileID: 0} - - target: {fileID: 7623484707498698197, guid: cd445451907d7274daea79c67e13a202, - type: 3} - propertyPath: gravityScale - value: 2 - objectReference: {fileID: 0} - - target: {fileID: 7623484707498698197, guid: cd445451907d7274daea79c67e13a202, - type: 3} - propertyPath: jumpForce - value: 10 - objectReference: {fileID: 0} - - target: {fileID: 7050504111816705868, guid: cd445451907d7274daea79c67e13a202, - type: 3} - propertyPath: m_SkinWidth - value: 0.0125 - objectReference: {fileID: 0} - target: {fileID: 6891912346901077427, guid: cd445451907d7274daea79c67e13a202, type: 3} propertyPath: collisionMask.m_Bits value: 2048 objectReference: {fileID: 0} - m_RemovedComponents: [] + - target: {fileID: 6383496741659177281, guid: cd445451907d7274daea79c67e13a202, + type: 3} + propertyPath: m_IsActive + value: 1 + objectReference: {fileID: 0} + m_RemovedComponents: + - {fileID: 528243339991412957, guid: cd445451907d7274daea79c67e13a202, type: 3} m_SourcePrefab: {fileID: 100100000, guid: cd445451907d7274daea79c67e13a202, type: 3} --- !u!1001 &8206000050524585146 PrefabInstance: diff --git a/Assets/Scripts/MoveController.cs b/Assets/Scripts/MoveController.cs index 9e4f2eb..b0e6d3e 100644 --- a/Assets/Scripts/MoveController.cs +++ b/Assets/Scripts/MoveController.cs @@ -9,6 +9,7 @@ public class MoveController : MonoBehaviour public const float skinWidth = 0.015f; public int horizontalRayCount = 4; public int verticalRayCount = 4; + public CollisionInfo collisions; new private BoxCollider collider; private RaycastOrigins raycastOrigins; @@ -22,33 +23,82 @@ public class MoveController : MonoBehaviour public void Move(Vector3 velocity) { UpdateRaycastOrigins(); + collisions.Reset(); + + HorizontalCollisions(ref velocity); VerticalCollisions(ref velocity); + // if (velocity.x != 0) { + // } + // if (velocity.y != 0) { + // } transform.Translate(velocity); } + private void HorizontalCollisions(ref Vector3 velocity) { + float directionX = Mathf.Sign(velocity.x); // -1 for left, 1 for right + float rayLength = Mathf.Abs(velocity.x) + skinWidth; + + for (int i = 0; i < horizontalRayCount; i++) { + for (int j = 0; j < horizontalRayCount; j++) { + Vector3 rayOrigin = (directionX == -1) ? raycastOrigins.bottomFrontLeft : raycastOrigins.bottomFrontRight; + rayOrigin += Vector3.up * (horizontalRaySpacing * i); + rayOrigin += Vector3.back * (horizontalRaySpacing * j); + RaycastHit[] hits = Physics.RaycastAll(rayOrigin, Vector3.right * directionX, rayLength, collisionMask); + + if (hits.Length > 0) { + float minHitDist = hits[0].distance; + foreach(RaycastHit hit in hits) { + if (hit.distance < minHitDist) { + minHitDist = hit.distance; + } + } + velocity.x = (minHitDist - skinWidth) * directionX; + Debug.LogFormat("with RayLength {0} MinHitDist {1} setting velocity.y to {2}", rayLength, minHitDist, velocity.y); + rayLength = minHitDist; + Debug.DrawRay(rayOrigin, Vector3.right * directionX * rayLength, Color.red); + + if (directionX == -1) { + collisions.left = true; + } else { + collisions.right = true; + } + } else { + Debug.DrawRay(rayOrigin, Vector3.right * directionX * rayLength, Color.green); + } + } + } + } + private void VerticalCollisions(ref Vector3 velocity) { - float directionY = Mathf.Sign(velocity.y); + float directionY = Mathf.Sign(velocity.y); // -1 for down, 1 for up float rayLength = Mathf.Abs(velocity.y) + skinWidth; - // bottom face for (int i = 0; i < verticalRayCount; i++) { for (int j = 0; j < verticalRayCount; j++) { Vector3 rayOrigin = (directionY == -1) ? raycastOrigins.bottomFrontLeft : raycastOrigins.topFrontLeft; rayOrigin += Vector3.right * (verticalRaySpacing * i + velocity.x); + rayOrigin += Vector3.forward * (verticalRaySpacing * j + velocity.x); RaycastHit[] hits = Physics.RaycastAll(rayOrigin, Vector3.up * directionY, rayLength, collisionMask); if (hits.Length > 0) { - float minHitDist = 0f; + float minHitDist = hits[0].distance; foreach(RaycastHit hit in hits) { - if (hit.distance > minHitDist) { + if (hit.distance < minHitDist) { minHitDist = hit.distance; } } velocity.y = (minHitDist - skinWidth) * directionY; + Debug.LogFormat("with RayLength {0} MinHitDist {1} setting velocity.y to {2}", rayLength, minHitDist, velocity.y); rayLength = minHitDist; - Debug.DrawRay(raycastOrigins.bottomFrontLeft + Vector3.right * verticalRaySpacing * i - Vector3.back * verticalRaySpacing * j, Vector3.down * 0.25f, Color.red); + Debug.DrawRay(rayOrigin, Vector3.up * directionY * rayLength, Color.red); + + if (directionY == -1) { + collisions.below = true; + } else { + collisions.above = true; + } } else { - Debug.DrawRay(raycastOrigins.bottomFrontLeft + Vector3.right * verticalRaySpacing * i - Vector3.back * verticalRaySpacing * j, Vector3.down * 0.25f, Color.green); + Debug.DrawRay(rayOrigin, Vector3.up * directionY * rayLength, Color.green); } } } @@ -90,4 +140,13 @@ public class MoveController : MonoBehaviour public Vector3 bottomFrontLeft; public Vector3 bottomFrontRight; } + + public struct CollisionInfo { + public bool above; + public bool below; + public bool left; + public bool right; + + public void Reset() { above = below = left = right = false; } + } } diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index 839acc1..2176fbd 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -4,16 +4,27 @@ using UnityEngine; [RequireComponent(typeof(MoveController))] public class PlayerController : MonoBehaviour { + + public float moveSpeed = 6f; public MoveController moveController; - private float gravity = -20; - private Vector3 velocity; + private float gravity = -20f; + public Vector3 velocity; void Start() { moveController = GetComponent(); } void Update() { + if (moveController.collisions.above || moveController.collisions.below) { + velocity.y = 0; + } + if (moveController.collisions.left || moveController.collisions.right) { + velocity.x = 0; + } + + Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); + velocity.x = input.x * moveSpeed; velocity.y += gravity * Time.deltaTime; moveController.Move(velocity * Time.deltaTime); } diff --git a/ProjectSettings/DynamicsManager.asset b/ProjectSettings/DynamicsManager.asset index 99831df..9bbf68c 100644 --- a/ProjectSettings/DynamicsManager.asset +++ b/ProjectSettings/DynamicsManager.asset @@ -1,30 +1,33 @@ -%YAML 1.1 -%TAG !u! tag:unity3d.com,2011: ---- !u!55 &1 -PhysicsManager: - m_ObjectHideFlags: 0 - serializedVersion: 8 - m_Gravity: {x: 0, y: -9.81, z: 0} - m_DefaultMaterial: {fileID: 0} - m_BounceThreshold: 2 - m_SleepThreshold: 0.005 - m_DefaultContactOffset: 0.01 - m_DefaultSolverIterations: 6 - m_DefaultSolverVelocityIterations: 1 - m_QueriesHitBackfaces: 0 - m_QueriesHitTriggers: 1 - m_EnableAdaptiveForce: 0 - m_ClothInterCollisionDistance: 0 - m_ClothInterCollisionStiffness: 0 - m_ContactsGeneration: 1 - m_LayerCollisionMatrix: ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - m_AutoSimulation: 1 - m_AutoSyncTransforms: 0 - m_ReuseCollisionCallbacks: 1 - m_ClothInterCollisionSettingsToggle: 0 - m_ContactPairsMode: 0 - m_BroadphaseType: 0 - m_WorldBounds: - m_Center: {x: 0, y: 0, z: 0} - m_Extent: {x: 250, y: 250, z: 250} - m_WorldSubdivisions: 8 +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!55 &1 +PhysicsManager: + m_ObjectHideFlags: 0 + serializedVersion: 10 + m_Gravity: {x: 0, y: -9.81, z: 0} + m_DefaultMaterial: {fileID: 0} + m_BounceThreshold: 2 + m_SleepThreshold: 0.005 + m_DefaultContactOffset: 0.01 + m_DefaultSolverIterations: 6 + m_DefaultSolverVelocityIterations: 1 + m_QueriesHitBackfaces: 0 + m_QueriesHitTriggers: 1 + m_EnableAdaptiveForce: 0 + m_ClothInterCollisionDistance: 0 + m_ClothInterCollisionStiffness: 0 + m_ContactsGeneration: 1 + m_LayerCollisionMatrix: ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + m_AutoSimulation: 1 + m_AutoSyncTransforms: 1 + m_ReuseCollisionCallbacks: 1 + m_ClothInterCollisionSettingsToggle: 0 + m_ContactPairsMode: 0 + m_BroadphaseType: 0 + m_WorldBounds: + m_Center: {x: 0, y: 0, z: 0} + m_Extent: {x: 250, y: 250, z: 250} + m_WorldSubdivisions: 8 + m_FrictionType: 0 + m_EnableEnhancedDeterminism: 0 + m_EnableUnifiedHeightmaps: 1