Created
July 16, 2015 01:21
-
-
Save grimmdev/aea429fc9cca292a2f0e to your computer and use it in GitHub Desktop.
Pokemon Move Controller in Unity 3d with 2D!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Sean Loper | |
// 2015 | |
// Also supports complete controller input! | |
// Pokemon Styled Movement Controller for Unity of course! | |
using UnityEngine; | |
using System.Collections; | |
public class MoveController : MonoBehaviour { | |
private enum moveState { Idle, Walking }; | |
private moveState MoveState = moveState.Idle; | |
private Vector3 playerInput = Vector3.zero; | |
private Vector3 nextPosition = Vector3.zero; | |
public LayerMask CollisionLayer; | |
private void Awake() | |
{ | |
// we don't want the player to go anywhere when the scene loads. | |
nextPosition = transform.position; | |
} | |
private void Update() | |
{ | |
LegacyMove (); | |
} | |
private void LegacyMove() | |
{ | |
if (MoveState == moveState.Idle) { | |
playerInput.x = Mathf.Round (Input.GetAxisRaw ("Horizontal")); | |
playerInput.y = Mathf.Round (Input.GetAxisRaw ("Vertical")); | |
// here we play or set Idle animation, but I'm using my own custom C# 2d Animation system. | |
} else { | |
// we use this to set the Walking animation here. | |
} | |
if (playerInput != Vector3.zero) { | |
nextPosition = transform.position; | |
if(downCheck() && playerInput.y < 0){ | |
playerInput.y = 0; | |
} | |
if(upCheck() && playerInput.y > 0){ | |
playerInput.y = 0; | |
} | |
if(leftCheck() && playerInput.x < 0){ | |
playerInput.x = 0; | |
} | |
if(rightCheck() && playerInput.x > 0){ | |
playerInput.x = 0; | |
} | |
nextPosition = transform.position + playerInput; | |
playerInput = Vector3.zero; | |
} | |
if (Vector3.Distance(transform.position,nextPosition) > 0.1f) { | |
transform.position = Vector3.Lerp (transform.position, nextPosition, 0.1f); | |
MoveState = moveState.Walking; | |
} else { | |
nextPosition.x = Mathf.Round(nextPosition.x); | |
nextPosition.y = Mathf.Round(nextPosition.y); | |
transform.position = nextPosition; | |
MoveState = moveState.Idle; | |
} | |
} | |
// we use different colors for each check, because RAINBOWS! | |
bool leftCheck() | |
{ | |
float extraRayLength = 0.1f; | |
Vector3 startOfRay = new Vector3(transform.position.x,transform.position.y + 0.5f,transform.position.z); | |
Vector3 endOfRay = startOfRay - (Vector3.right* (extraRayLength + gameObject.GetComponent<Collider2D>().bounds.size.y/2.0f) ); | |
Debug.DrawLine( startOfRay, endOfRay, Color.blue ); | |
RaycastHit2D hitLeft = Physics2D.Linecast( startOfRay, endOfRay, CollisionLayer ); | |
return hitLeft; | |
} | |
bool rightCheck() | |
{ | |
float extraRayLength = 0.1f; | |
Vector3 startOfRay = new Vector3(transform.position.x + 1,transform.position.y + 0.5f,transform.position.z); | |
Vector3 endOfRay = startOfRay - (Vector3.left* (extraRayLength + gameObject.GetComponent<Collider2D>().bounds.size.y/2.0f) ); | |
Debug.DrawLine( startOfRay, endOfRay, Color.red ); | |
RaycastHit2D hitRight = Physics2D.Linecast( startOfRay, endOfRay, CollisionLayer ); | |
return hitRight; | |
} | |
bool upCheck() | |
{ | |
float extraRayLength = 0.1f; | |
Vector3 startOfRay = new Vector3(transform.position.x + 0.5f,transform.position.y + 1,transform.position.z); | |
Vector3 endOfRay = startOfRay - (Vector3.down* (extraRayLength + gameObject.GetComponent<Collider2D>().bounds.size.y/2.0f) ); | |
Debug.DrawLine( startOfRay, endOfRay, Color.green ); | |
RaycastHit2D hitUp = Physics2D.Linecast( startOfRay, endOfRay, CollisionLayer ); | |
return hitUp; | |
} | |
bool downCheck() | |
{ | |
float extraRayLength = 0.1f; | |
Vector3 startOfRay = new Vector3(transform.position.x + 0.5f,transform.position.y,transform.position.z); | |
Vector3 endOfRay = startOfRay - (Vector3.up* (extraRayLength + gameObject.GetComponent<Collider2D>().bounds.size.y/2.0f) ); | |
Debug.DrawLine( startOfRay, endOfRay, Color.yellow ); | |
RaycastHit2D hitDown = Physics2D.Linecast( startOfRay, endOfRay, CollisionLayer ); | |
return hitDown; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment