Skip to content

Instantly share code, notes, and snippets.

@StefanoFiumara
Created September 6, 2013 06:03
Show Gist options
  • Save StefanoFiumara/6460085 to your computer and use it in GitHub Desktop.
Save StefanoFiumara/6460085 to your computer and use it in GitHub Desktop.
Re-written movement script, old script still at the bottom just in case!
using UnityEngine;
using System.Collections;
public class GridMove : MonoBehaviour {
public enum PlayerState {MOVING, STOPPED, TURNING};
public enum Direction {UP, DOWN, LEFT, RIGHT};
public PlayerState state;
public Direction facing;
public float speed;
public float turnRange;
private Vector3 moveLeft, moveUp, moveRight, moveDown;
// Use this for initialization
void Start () {
//must assign an initial state
state = PlayerState.STOPPED;
facing = Direction.RIGHT;
//set the direction vectors
moveUp = new Vector3(0, 0, speed);
moveDown = new Vector3(0, 0, -speed);
moveLeft = new Vector3(-speed, 0, 0);
moveRight = new Vector3(speed, 0, 0);
}
// Update is called once per frame
void Update () {
//Input only affects the state
if(Input.GetKeyDown("up")) {
if(state != PlayerState.TURNING && facing != Direction.DOWN) {
state = PlayerState.TURNING;
facing = Direction.UP;
}
} else if(Input.GetKeyDown("left")) {
if(state != PlayerState.TURNING && facing != Direction.RIGHT) {
state = PlayerState.TURNING;
facing = Direction.LEFT;
}
} else if(Input.GetKeyDown("down")) {
if(state != PlayerState.TURNING && facing != Direction.UP) {
state = PlayerState.TURNING;
facing = Direction.DOWN;
}
} else if(Input.GetKeyDown("right")) {
if(state != PlayerState.TURNING && facing != Direction.LEFT) {
state = PlayerState.TURNING;
facing = Direction.RIGHT;
}
}
switch(state) {
case PlayerState.MOVING:
movePlayer();
break;
case PlayerState.STOPPED:
rigidbody.velocity = Vector3.zero;
break;
case PlayerState.TURNING:
checkTurning();
break;
}
}
void movePlayer() {
/*
* TO DO:
* -setting rigidbody.velocity directly is not recommened, there's probably a better way, but I'm not sure how rigidbody.AddForce works.
*/
switch(facing) {
case Direction.UP:
rigidbody.velocity = moveUp;
break;
case Direction.DOWN:
rigidbody.velocity = moveDown;
break;
case Direction.LEFT:
rigidbody.velocity = moveLeft;
break;
case Direction.RIGHT:
rigidbody.velocity = moveRight;
break;
}
}
/**
* Checks if the player is within range to turn and stay aligned to the grid before switching states.
*/
void checkTurning() {
Vector3 oldPos = transform.position;
if(facing == Direction.LEFT || facing == Direction.RIGHT) {
//check if player is lined up to the depth ("vertical") axis
if(transform.position.z - Mathf.Floor(transform.position.z) < turnRange) {
//set position to abs value
iTween.MoveTo(gameObject, iTween.Hash("z", Mathf.Round(transform.position.z), "time", 0.05));
//transform.position = new Vector3(oldPos.x, oldPos.y, Mathf.Round(transform.position.z));
//successful turn, back to moving.
state = PlayerState.MOVING;
}
}
else {
if(transform.position.x - Mathf.Floor(transform.position.x) < turnRange) {
iTween.MoveTo(gameObject, iTween.Hash("x", Mathf.Round(transform.position.x), "time", 0.1));
//transform.position = new Vector3(Mathf.Round(transform.position.x), oldPos.y, oldPos.z);
state = PlayerState.MOVING;
}
}
}
void OnCollisionEnter(Collision col) {
if(col.gameObject.name.Contains("Left Bumper")) {
state = PlayerState.STOPPED;
Debug.Log("Hit Left Bumper!");
}
if(col.gameObject.name.Contains("Right Bumper")) {
state = PlayerState.STOPPED;
Debug.Log("Hit Right Bumper!");
}
if(col.gameObject.name.Contains("Top Bumper")) {
state = PlayerState.STOPPED;
Debug.Log("Hit Top Bumper!");
}
if(col.gameObject.name.Contains("Bottom Bumper")) {
state = PlayerState.STOPPED;
Debug.Log("Hit Bottom Bumper!");
}
}
}
/********************/
//Old GridMove Code in case we want to revert
/********************/
/*
using System.Collections;
using UnityEngine;
class GridMove : MonoBehaviour
{
public float moveSpeed = 3f;
public float gridSize = 1f;
private enum Orientation
{
Horizontal,
Vertical
};
private Orientation gridOrientation = Orientation.Horizontal;
private bool allowDiagonals = false;
private bool correctDiagonalSpeed = true;
private Vector2 input;
private bool isMoving = false;
private Vector3 startPosition;
private Vector3 endPosition;
private float t;
private float factor;
public void Update()
{
if (!isMoving)
{
input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
if (!allowDiagonals)
{
if (Mathf.Abs(input.x) > Mathf.Abs(input.y))
{
input.y = 0;
} else {
input.x = 0;
}
}
if (input != Vector2.zero)
{
StartCoroutine(move(transform));
}
}
}
public IEnumerator move(Transform transform)
{
isMoving = true;
startPosition = transform.position;
t = 0;
if(gridOrientation == Orientation.Horizontal)
{
endPosition = new Vector3(startPosition.x + System.Math.Sign(input.x) * gridSize,
startPosition.y, startPosition.z + System.Math.Sign(input.y) * gridSize);
} else
{
endPosition = new Vector3(startPosition.x + System.Math.Sign(input.x) * gridSize,
startPosition.y + System.Math.Sign(input.y) * gridSize, startPosition.z);
}
if(allowDiagonals && correctDiagonalSpeed && input.x != 0 && input.y != 0) {
factor = 0.7071f;
} else
{
factor = 1f;
}
while (t < 1f)
{
t += Time.deltaTime * (moveSpeed/gridSize) * factor;
transform.position = Vector3.Lerp(startPosition, endPosition, t);
yield return null;
}
isMoving = false;
yield return 0;
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment