From 62312db4b0f7d3bd3a2b294a1cabc6131950fbea Mon Sep 17 00:00:00 2001 From: etzarov Date: Wed, 20 May 2020 18:16:45 -0500 Subject: [PATCH] Updated to work with MVC pattern, added comments --- Assets/NinjaController.cs | 30 +++++++++++++ Assets/NinjaController.cs.meta | 11 +++++ Assets/NinjaView.cs | 38 ++++++++++++++++ Assets/NinjaView.cs.meta | 11 +++++ Assets/Scenes/SampleScene.unity | 36 +++++++++++++-- Assets/Script/Ninja.cs | 78 +++++++++++++++------------------ 6 files changed, 157 insertions(+), 47 deletions(-) create mode 100644 Assets/NinjaController.cs create mode 100644 Assets/NinjaController.cs.meta create mode 100644 Assets/NinjaView.cs create mode 100644 Assets/NinjaView.cs.meta diff --git a/Assets/NinjaController.cs b/Assets/NinjaController.cs new file mode 100644 index 0000000..d6b385a --- /dev/null +++ b/Assets/NinjaController.cs @@ -0,0 +1,30 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class NinjaController : MonoBehaviour +{ + Ninja ninjaModel; + // Start is called before the first frame update + void Start() + { + ninjaModel = GetComponent(); + } + + + void Update() + { + UpdateMovementInput(); + } + + void UpdateMovementInput() + { + ninjaModel.moveX = Input.GetAxis("Horizontal"); + + //If jump button, is pushed, execute jump function + if (Input.GetButtonDown("Jump")) + { + ninjaModel.Jump(); + } + } +} diff --git a/Assets/NinjaController.cs.meta b/Assets/NinjaController.cs.meta new file mode 100644 index 0000000..497bd04 --- /dev/null +++ b/Assets/NinjaController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: da252e95c040c9e4c8061061074031d6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/NinjaView.cs b/Assets/NinjaView.cs new file mode 100644 index 0000000..3c0c7b0 --- /dev/null +++ b/Assets/NinjaView.cs @@ -0,0 +1,38 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class NinjaView : MonoBehaviour +{ + Animator animator; + SpriteRenderer sprite; + + // Start is called before the first frame update + void Start() + { + animator = GetComponent(); + sprite = GetComponent(); + } + + + public void UpdateAnimator(float moveX, bool grounded) + { + + //Based on the direction of MoveX, Flip the Sprite + //If the move is so tiny (e.g.) small move, then ignore flipping + float smallMove = 0.0001f; + if (moveX < -smallMove) + { + sprite.flipX = true; + } + else if (moveX > smallMove) + { + sprite.flipX = false; + } + + // Animator parameters + //Sets the animation paramaters /position of the character based on its speed and groundedness + animator.SetFloat("speed", Mathf.Abs(moveX)); + animator.SetBool("grounded", grounded); + } +} diff --git a/Assets/NinjaView.cs.meta b/Assets/NinjaView.cs.meta new file mode 100644 index 0000000..0b4fb48 --- /dev/null +++ b/Assets/NinjaView.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3f90f732f2d4c6e41b1064dffd28b0ef +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index fb4d9a3..3adfeab 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -38,7 +38,7 @@ RenderSettings: m_ReflectionIntensity: 1 m_CustomReflection: {fileID: 0} m_Sun: {fileID: 705507994} - m_IndirectSpecularColor: {r: 0.44657868, g: 0.4964124, b: 0.574817, a: 1} + m_IndirectSpecularColor: {r: 0.44657898, g: 0.4964133, b: 0.5748178, a: 1} m_UseRadianceAmbientProbe: 0 --- !u!157 &3 LightmapSettings: @@ -310,6 +310,8 @@ GameObject: - component: {fileID: 1571386447} - component: {fileID: 1571386446} - component: {fileID: 1571386445} + - component: {fileID: 1571386448} + - component: {fileID: 1571386449} m_Layer: 0 m_Name: Ninja m_TagString: Player @@ -446,7 +448,7 @@ Rigidbody2D: m_Interpolate: 0 m_SleepingMode: 1 m_CollisionDetection: 0 - m_Constraints: 0 + m_Constraints: 4 --- !u!114 &1571386447 MonoBehaviour: m_ObjectHideFlags: 0 @@ -459,9 +461,35 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: de5ea80d1091547beaa63a3993619707, type: 3} m_Name: m_EditorClassIdentifier: + moveX: 0 + grounded: 0 speed: 10 jumpspeed: 20 maxJumpTime: 0.2 +--- !u!114 &1571386448 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1571386441} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: da252e95c040c9e4c8061061074031d6, type: 3} + m_Name: + m_EditorClassIdentifier: +--- !u!114 &1571386449 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1571386441} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 3f90f732f2d4c6e41b1064dffd28b0ef, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1 &1842770162 GameObject: m_ObjectHideFlags: 0 @@ -586,8 +614,8 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1842770162} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0.39, y: -3.17, z: 0} - m_LocalScale: {x: 2, y: 1, z: 1} + m_LocalPosition: {x: -0.01, y: -3.17, z: 0} + m_LocalScale: {x: 3.7220402, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} m_RootOrder: 3 diff --git a/Assets/Script/Ninja.cs b/Assets/Script/Ninja.cs index 2747a07..66cfe59 100644 --- a/Assets/Script/Ninja.cs +++ b/Assets/Script/Ninja.cs @@ -4,8 +4,8 @@ public class Ninja : MonoBehaviour { - float moveX; - bool grounded; + [HideInInspector] public float moveX; + [HideInInspector] public bool grounded; bool jumping; float jumpTime; @@ -15,74 +15,66 @@ public class Ninja : MonoBehaviour public float jumpspeed = 1.0f; public float maxJumpTime = 1.0f; - Animator animator; Rigidbody2D rb; BoxCollider2D boxCollider; - SpriteRenderer sprite; + NinjaView ninjaView; void Start() { - animator = GetComponent(); + ninjaView = GetComponent(); rb = GetComponent(); boxCollider = GetComponent(); - sprite = GetComponent(); + } // Update is called once per frame void Update() { - // Is platform below us? - float selfHeightOffset = (boxCollider.size.y / 2.0f) + 0.1f; - float rayLen = 0.05f; - Vector2 pos2D = (Vector2)transform.position; - // Debug.DrawRay(pos2D - (Vector2.up * selfHeightOffset), -Vector2.up * rayLen, Color.red); - RaycastHit2D hit = Physics2D.Raycast(pos2D - (Vector2.up * selfHeightOffset), -Vector2.up, rayLen); - - grounded = false; - if (hit.collider != null) - { - if (hit.collider.CompareTag("Platform")) - { - grounded = true; - } - } + DetectGround(); - // Jumping + // Jumping if (grounded) { - moveX = Input.GetAxis("Horizontal"); - if (jumpTime < 0.0f) { jumping = false; } } + jumpTime -= Time.deltaTime; + //Updates the animator based on current movement. + ninjaView.UpdateAnimator(moveX, grounded); - bool jumpPressed = Input.GetButtonDown("Jump"); - if (grounded && jumpPressed && !jumping) - { - jumping = true; - jumpTime = maxJumpTime; - } + } - jumpTime -= Time.deltaTime; + void DetectGround() + { + // Is platform below us? Get raycast based on position directly below height of the player's box collider + float selfHeightOffset = (boxCollider.size.y / 2.0f) + 0.1f; + float rayLen = 0.05f; + Vector2 pos2D = (Vector2)transform.position; + // Debug.DrawRay(pos2D - (Vector2.up * selfHeightOffset), -Vector2.up * rayLen, Color.red); + RaycastHit2D hit = Physics2D.Raycast(pos2D - (Vector2.up * selfHeightOffset), -Vector2.up, rayLen); - // Reverse horizontal - float smallMove = 0.0001f; - if (moveX < -smallMove) - { - sprite.flipX = true; - } - else if (moveX > smallMove) + grounded = false; + //If the raycast is touching the platform, then character is grounded and stop gravity from occuring. + if (hit.collider != null) { - sprite.flipX = false; - } + if (hit.collider.CompareTag("Platform")) + { + grounded = true; + } + } + } - // Animator parameters - animator.SetFloat("speed", Mathf.Abs(moveX)); - animator.SetBool("grounded", grounded); + public void Jump() + { + if (grounded && !jumping) + { + jumping = true; + jumpTime = maxJumpTime; + } } // Physic stuff