Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Assets/NinjaController.cs
Original file line number Diff line number Diff line change
@@ -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<Ninja>();
}


void Update()
{
UpdateMovementInput();
}

void UpdateMovementInput()
{
ninjaModel.moveX = Input.GetAxis("Horizontal");

//If jump button, is pushed, execute jump function
if (Input.GetButtonDown("Jump"))
{
ninjaModel.Jump();
}
}
}
11 changes: 11 additions & 0 deletions Assets/NinjaController.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions Assets/NinjaView.cs
Original file line number Diff line number Diff line change
@@ -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<Animator>();
sprite = GetComponent<SpriteRenderer>();
}


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);
}
}
11 changes: 11 additions & 0 deletions Assets/NinjaView.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 32 additions & 4 deletions Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
78 changes: 35 additions & 43 deletions Assets/Script/Ninja.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

public class Ninja : MonoBehaviour
{
float moveX;
bool grounded;
[HideInInspector] public float moveX;
[HideInInspector] public bool grounded;
bool jumping;

float jumpTime;
Expand All @@ -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<Animator>();
ninjaView = GetComponent<NinjaView>();
rb = GetComponent<Rigidbody2D>();
boxCollider = GetComponent<BoxCollider2D>();
sprite = GetComponent<SpriteRenderer>();

}

// 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
Expand Down