From 5fd14b8d9b1f1e889506741a5355b8c8ba697dcd Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sun, 26 Jan 2020 15:43:16 -0600 Subject: [PATCH] trying the sebastian lague technique --- Assets/Prefabs/Player.prefab | 51 +++++++++------- Assets/Scripts/MoveController.cs | 69 +++++++++++++++++++++ Assets/Scripts/MoveController.cs.meta | 11 ++++ Assets/Scripts/PlayerController.cs | 28 +-------- ProjectSettings/TagManager.asset | 86 +++++++++++++-------------- 5 files changed, 155 insertions(+), 90 deletions(-) create mode 100644 Assets/Scripts/MoveController.cs create mode 100644 Assets/Scripts/MoveController.cs.meta diff --git a/Assets/Prefabs/Player.prefab b/Assets/Prefabs/Player.prefab index 5ffa9b5..14a02cb 100644 --- a/Assets/Prefabs/Player.prefab +++ b/Assets/Prefabs/Player.prefab @@ -9,11 +9,12 @@ GameObject: serializedVersion: 6 m_Component: - component: {fileID: 6383496741192187647} - - component: {fileID: 7623484707498698197} - - component: {fileID: 7050504111816705868} - m_Layer: 0 + - component: {fileID: 8462409473881997938} + - component: {fileID: 6891912346901077427} + - component: {fileID: 5588890751565926871} + m_Layer: 10 m_Name: Player - m_TagString: Untagged + m_TagString: Player m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 @@ -33,7 +34,20 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!114 &7623484707498698197 +--- !u!65 &8462409473881997938 +BoxCollider: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 6383496741192187632} + m_Material: {fileID: 0} + m_IsTrigger: 0 + m_Enabled: 1 + serializedVersion: 2 + m_Size: {x: 0.25, y: 1, z: 0.25} + m_Center: {x: 0, y: 0.5, z: 0} +--- !u!114 &6891912346901077427 MonoBehaviour: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} @@ -42,30 +56,23 @@ MonoBehaviour: m_GameObject: {fileID: 6383496741192187632} m_Enabled: 1 m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 7e392b35ca7959444b61a3e29a242f2b, type: 3} + m_Script: {fileID: 11500000, guid: 721dfc7e43d81b644a34024174f78c4e, type: 3} m_Name: m_EditorClassIdentifier: - moveSpeed: 1 - jumpForce: 10 - cc: {fileID: 0} ---- !u!143 &7050504111816705868 -CharacterController: + box: {fileID: 0} +--- !u!114 &5588890751565926871 +MonoBehaviour: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 6383496741192187632} - m_Material: {fileID: 0} - m_IsTrigger: 0 m_Enabled: 1 - serializedVersion: 2 - m_Height: 1 - m_Radius: 0.125 - m_SlopeLimit: 45 - m_StepOffset: 0.3 - m_SkinWidth: 0.08 - m_MinMoveDistance: 0.001 - m_Center: {x: 0, y: 0.5, z: 0} + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 7e392b35ca7959444b61a3e29a242f2b, type: 3} + m_Name: + m_EditorClassIdentifier: + moveController: {fileID: 0} --- !u!1 &6383496741659177281 GameObject: m_ObjectHideFlags: 0 @@ -77,7 +84,7 @@ GameObject: - component: {fileID: 6383496741659177293} - component: {fileID: 6383496741659177294} - component: {fileID: 6383496741659177295} - m_Layer: 0 + m_Layer: 10 m_Name: Body m_TagString: Untagged m_Icon: {fileID: 0} diff --git a/Assets/Scripts/MoveController.cs b/Assets/Scripts/MoveController.cs new file mode 100644 index 0000000..83b351c --- /dev/null +++ b/Assets/Scripts/MoveController.cs @@ -0,0 +1,69 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +[RequireComponent(typeof(BoxCollider))] +public class MoveController : MonoBehaviour +{ + public const float skinWidth = 0.015f; + public int horizontalRayCount = 4; + public int verticalRayCount = 4; + + new private BoxCollider collider; + private RaycastOrigins raycastOrigins; + private float horizontalRaySpacing; + private float verticalRaySpacing; + + void Start() { + collider = GetComponent(); + } + + void Update() { + UpdateRaycastOrigins(); + CalculateRaySpacing(); + + // bottom face + for (int i = 0; i < verticalRayCount; i++) { + for (int j = 0; j < verticalRayCount; j++) { + Debug.DrawRay(raycastOrigins.bottomFrontLeft + Vector3.right * verticalRaySpacing * i - Vector3.back * verticalRaySpacing * j, Vector3.down, Color.red); + } + } + } + + void UpdateRaycastOrigins() { + Bounds bounds = collider.bounds; + bounds.Expand(skinWidth * -2); + + raycastOrigins.bottomFrontLeft = new Vector3(bounds.min.x, bounds.min.y, bounds.min.z); // 0 0 0 + raycastOrigins.bottomBackLeft = new Vector3(bounds.min.x, bounds.min.y, bounds.max.z); // 0 0 1 + raycastOrigins.topFrontLeft = new Vector3(bounds.min.x, bounds.max.y, bounds.min.z); // 0 1 0 + raycastOrigins.topBackLeft = new Vector3(bounds.min.x, bounds.max.y, bounds.max.z); // 0 1 1 + + 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() { + Bounds bounds = collider.bounds; + bounds.Expand(skinWidth * -2); + + if (horizontalRayCount < 2) { horizontalRayCount = 2; } + if (verticalRayCount < 2) { verticalRayCount = 2; } + + horizontalRaySpacing = bounds.size.y / (horizontalRayCount - 1); + verticalRaySpacing = bounds.size.x / (verticalRayCount - 1); + } + + struct RaycastOrigins { + public Vector3 topBackLeft; + public Vector3 topBackRight; + public Vector3 topFrontLeft; + public Vector3 topFrontRight; + public Vector3 bottomBackLeft; + public Vector3 bottomBackRight; + public Vector3 bottomFrontLeft; + public Vector3 bottomFrontRight; + } +} diff --git a/Assets/Scripts/MoveController.cs.meta b/Assets/Scripts/MoveController.cs.meta new file mode 100644 index 0000000..d1b576f --- /dev/null +++ b/Assets/Scripts/MoveController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 721dfc7e43d81b644a34024174f78c4e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index fdfdf28..0d00a07 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -2,39 +2,17 @@ using System.Collections.Generic; using UnityEngine; +[RequireComponent(typeof(MoveController))] public class PlayerController : MonoBehaviour { - public float moveSpeed; - public float jumpForce; - public CharacterController cc; - public float gravityScale; - - private Vector3 moveDirection; + public MoveController moveController; void Start() { - cc = GetComponent(); + moveController = GetComponent(); } void Update() { - float dx = Input.GetAxis("Horizontal") * moveSpeed; - moveDirection = new Vector3(dx, moveDirection.y, 0f); - if (cc.isGrounded) { - if (Input.GetButtonDown("Jump")) { - moveDirection.y = jumpForce; - } else { - moveDirection.y = moveDirection.y + Physics.gravity.y*gravityScale*Time.deltaTime; - if (moveDirection.y < 0f) { - moveDirection.y = 0f; - } - } - } else { - moveDirection.y = moveDirection.y + Physics.gravity.y*gravityScale*Time.deltaTime; - } - Debug.Log(moveDirection); - cc.Move(moveDirection * Time.deltaTime); } void FixedUpdate() { - float motor = Input.GetAxis("Vertical"); - float steering = Input.GetAxis("Horizontal"); } } diff --git a/ProjectSettings/TagManager.asset b/ProjectSettings/TagManager.asset index 5ce97fb..9bdf0f3 100644 --- a/ProjectSettings/TagManager.asset +++ b/ProjectSettings/TagManager.asset @@ -1,43 +1,43 @@ -%YAML 1.1 -%TAG !u! tag:unity3d.com,2011: ---- !u!78 &1 -TagManager: - serializedVersion: 2 - tags: [] - layers: - - Default - - TransparentFX - - Ignore Raycast - - - - Water - - UI - - - - - - PostProcessing - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - m_SortingLayers: - - name: Default - uniqueID: 0 - locked: 0 +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!78 &1 +TagManager: + serializedVersion: 2 + tags: [] + layers: + - Default + - TransparentFX + - Ignore Raycast + - + - Water + - UI + - + - + - PostProcessing + - + - Player + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + m_SortingLayers: + - name: Default + uniqueID: 0 + locked: 0