Skip to content

Commit

Permalink
Rough 2d platformer is done. Logic inspired by
Browse files Browse the repository at this point in the history
  • Loading branch information
lustdante committed Dec 14, 2013
1 parent 073fd01 commit 5fdbfd4
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 6 deletions.
55 changes: 53 additions & 2 deletions UnityProject/Assets/Scripts/PlayerController.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,66 @@
using UnityEngine;
using System.Collections;

[RequireComponent(typeof(PlayerPhysics))]
[RequireComponent(typeof(tk2dSpriteAnimator))]
public class PlayerController : MonoBehaviour {

// Player variables
public float gravity = 20;
public float speed = 12;
public float acceleration = 30;
public float jumpHeight = 12;

private float currentSpeed;
private float targetSpeed;
private Vector2 delta;
private float moveDirX;

// States
private bool jumping;
private bool climbing;

private PlayerPhysics playerPhysics;
tk2dSpriteAnimator animator;

// Use this for initialization
void Start () {

playerPhysics = GetComponent<PlayerPhysics>();
animator = GetComponent<tk2dSpriteAnimator>();
}

// Update is called once per frame
void Update () {

if(playerPhysics.stopped){
targetSpeed = 0;
currentSpeed = 0;
}

if(playerPhysics.grounded){
delta.y = 0;
}

if(Input.GetButtonDown("Jump")) {
if(playerPhysics.grounded) {
delta.y = jumpHeight;
}
}
moveDirX = Input.GetAxisRaw("Horizontal");
targetSpeed = moveDirX * speed;
currentSpeed = IncrementTowards(currentSpeed, targetSpeed, acceleration);

delta.x = currentSpeed;
delta.y -= gravity * Time.deltaTime;
playerPhysics.Move(delta * Time.deltaTime, moveDirX);
}

private float IncrementTowards(float n, float target, float a) {
if(n == target) {
return n;
} else {
float dir = Mathf.Sign (target - n);
n += a * Time.deltaTime * dir;
return (dir == Mathf.Sign(target-n))? n : target;
}
}
}
99 changes: 95 additions & 4 deletions UnityProject/Assets/Scripts/PlayerPhysics.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,106 @@
using UnityEngine;
using System.Collections;


// As inpired by YouTube Platformer Tutorial
[RequireComponent (typeof(BoxCollider))]
public class PlayerPhysics : MonoBehaviour {
public LayerMask collisionMask;

private BoxCollider collider;
private Vector3 s;
private Vector3 c;

private Vector3 originalSize;
private Vector3 originalCentre;
private float colliderScale;

private int divX = 3;
private int divY = 3;

private float skin = .005f;

[HideInInspector]
public bool grounded;
[HideInInspector]
public bool stopped;

Ray ray;
RaycastHit hit;

// Use this for initialization
void Start () {

collider = GetComponent<BoxCollider>();
colliderScale = transform.localScale.x;

originalSize = collider.size;
originalCentre = collider.center;
SetCollider(originalSize,originalCentre);
}

// Update is called once per frame
void Update () {

public void Move(Vector2 moveAmount, float moveDirX){
float deltaY = moveAmount.y;
float deltaX = moveAmount.x;
Vector2 p = transform.position;

grounded = false;

for(int i = 0; i<divX; i+=1){
float dir = Mathf.Sign(deltaY);
float x = (p.x + c.x - s.x/2) + s.x/(divX - 1) * i;
float y = p.y + c.y + s.y/2 * dir;

ray = new Ray(new Vector2(x,y), new Vector2(0, dir));
Debug.DrawRay(ray.origin,ray.direction);

if(Physics.Raycast(ray, out hit, Mathf.Abs (deltaY) + skin, collisionMask)) {
float dst = Vector3.Distance(ray.origin, hit.point);
deltaY = (dst > skin) ? (dst - skin) * dir : 0;
grounded = true;
break;
}
}

stopped = false;
if(deltaX != 0){
for(int i = 0;i<divY; i+=1){
float dir = Mathf.Sign(deltaX);
float x = p.x + c.x + s.x/2 * dir;
float y = p.y + c.y - s.y/2 + s.y/(divY-1) * i;

ray = new Ray(new Vector2(x,y), new Vector2(dir, 0));
Debug.DrawRay(ray.origin,ray.direction);

if(Physics.Raycast(ray, out hit, Mathf.Abs (deltaX) + skin, collisionMask)) {
float dst = Vector3.Distance(ray.origin, hit.point);
deltaX = (dst > skin) ? (dst - skin) * dir : 0;
stopped = true;
break;
}
}
}

// Literally the edge case
if(!grounded && !stopped){
Vector3 pdir = new Vector3(deltaX, deltaY);
Vector3 o = new Vector3(p.x + c.x + s.x/2 * Mathf.Sign (deltaX), p.y + c.y + s.y/2 * Mathf.Sign (deltaY));
ray = new Ray(o, pdir.normalized);

if(Physics.Raycast(ray, Mathf.Sqrt(deltaX * deltaX + deltaY * deltaY), collisionMask)) {
grounded = true;
deltaY = 0;
}
}

Vector2 value = new Vector2(deltaX, deltaY);
transform.Translate(value, Space.World);
}

public void SetCollider(Vector3 size, Vector3 centre) {
collider.size = size;
collider.center = centre;

s = size * colliderScale;
c = centre * colliderScale;
}
}
12 changes: 12 additions & 0 deletions UnityProject/UnityProject.userprefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Properties>
<MonoDevelop.Ide.Workspace />
<MonoDevelop.Ide.Workbench ActiveDocument="Assets\Scripts\GameEngine.cs">
<Files>
<File FileName="Assets\Scripts\GameEngine.cs" Line="9" Column="3" />
</Files>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints>
<BreakpointStore />
</MonoDevelop.Ide.DebuggingService.Breakpoints>
<MonoDevelop.Ide.DebuggingService.PinnedWatches />
</Properties>

0 comments on commit 5fdbfd4

Please sign in to comment.