Not sure what I should be grasping from the links information because the downloadable file doesn't seem to load in unity. This is my code right now and it some what accomplishes what I wanted to do but I would like to use the collisders in a way that if another collider has a point that is the same with the players collider, then figure out how to stop the player from moving in that direction. With my code I still get some instances where the player is somewhat in an area he shouldn't be but other then that it works fine. Any improvements on this code is also appreciated.
using UnityEngine;
using System.Collections;
public class PlayerScriptV2 : MonoBehaviour {
#region variables
public tk2dSpriteAnimator currentAnimation;
public tk2dTileMap tileMapFinder;
#region movement variables
public bool wasWDown = false;
public bool wasADown = false;
public bool wasSDown = false;
public bool wasDDown = false;
public bool isRunning = false;
public float iSpeed = 1.0f;
public bool blockedL = false;
public bool blockedR = false;
public bool blockedU = false;
public bool blockedD = false;
public int currentTileId;
public int currentTileX;
public int currentTileY;
public int leftMiddleTileId;
public int leftTopTileId;
public int leftBottomTileId;
public int rightMiddleTileId;
public int rightTopTileId;
public int rightBottomTileId;
public int upMiddleTileId;
public int upLeftTileId;
public int upRightTileId;
public int downMiddleTileId;
public int downLeftTileId;
public int downRightTileId;
#endregion
#endregion
// Use this for initialization
void Start () {
currentAnimation = GetComponent<tk2dSpriteAnimator>();
tileMapFinder = tk2dTileMap.FindObjectOfType(typeof(tk2dTileMap)) as tk2dTileMap;
}
// Update is called once per frame
void Update () {
tileMapFinder = tk2dTileMap.FindObjectOfType(typeof(tk2dTileMap)) as tk2dTileMap;
#region motion control
#region load needed variables
currentTileId = tileMapFinder.GetTileIdAtPosition(gameObject.collider.bounds.center, 0);
tileMapFinder.GetTileAtPosition(gameObject.collider.bounds.center, out currentTileX, out currentTileY);
//collider draws from bottom-left to top-right
leftTopTileId = tileMapFinder.GetTileIdAtPosition(gameObject.collider.bounds.min, 0);
leftMiddleTileId = tileMapFinder.GetTileIdAtPosition(new Vector3(gameObject.collider.bounds.min.x,gameObject.collider.bounds.center.y, gameObject.collider.bounds.center.z) , 0);
leftBottomTileId = tileMapFinder.GetTileIdAtPosition(new Vector3(gameObject.collider.bounds.min.x,gameObject.collider.bounds.max.y, gameObject.collider.bounds.center.z) , 0);
rightTopTileId = tileMapFinder.GetTileIdAtPosition(new Vector3(gameObject.collider.bounds.max.x, gameObject.collider.bounds.min.y, gameObject.collider.bounds.max.z), 0);
rightMiddleTileId = tileMapFinder.GetTileIdAtPosition(new Vector3(gameObject.collider.bounds.max.x,gameObject.collider.bounds.center.y, gameObject.collider.bounds.center.z) , 0);
rightBottomTileId = tileMapFinder.GetTileIdAtPosition(new Vector3(gameObject.collider.bounds.max.x,gameObject.collider.bounds.max.y, gameObject.collider.bounds.center.z) , 0);
upLeftTileId = tileMapFinder.GetTileIdAtPosition(new Vector3(gameObject.collider.bounds.min.x, gameObject.collider.bounds.max.y, gameObject.collider.bounds.center.z), 0);
upMiddleTileId = tileMapFinder.GetTileIdAtPosition(new Vector3(gameObject.collider.bounds.center.x,gameObject.collider.bounds.max.y, gameObject.collider.bounds.center.z) , 0);
upRightTileId = tileMapFinder.GetTileIdAtPosition(new Vector3(gameObject.collider.bounds.max.x, gameObject.collider.bounds.max.y, gameObject.collider.bounds.center.z), 0);
downLeftTileId = tileMapFinder.GetTileIdAtPosition(new Vector3(gameObject.collider.bounds.min.x, gameObject.collider.bounds.min.y, gameObject.collider.bounds.center.z), 0);
downMiddleTileId = tileMapFinder.GetTileIdAtPosition(new Vector3(gameObject.collider.bounds.center.x,gameObject.collider.bounds.min.y, gameObject.collider.bounds.center.z) , 0);
downRightTileId = tileMapFinder.GetTileIdAtPosition(new Vector3(gameObject.collider.bounds.max.x, gameObject.collider.bounds.min.y, gameObject.collider.bounds.center.z), 0);
#endregion
#region if wall is blocked test
//leftBlock test
if((leftMiddleTileId == 12 && leftTopTileId == 12 && leftBottomTileId == 12) || (leftMiddleTileId == 12 && leftTopTileId != 12 && leftBottomTileId == 12) || (leftMiddleTileId == 12 && leftTopTileId == 12 && leftBottomTileId != 12))
{
blockedL = true;
}
else
{
blockedL = false;
}
//rightBlock test
if((rightMiddleTileId == 12 && rightTopTileId == 12 && rightBottomTileId == 12) || (rightMiddleTileId == 12 && rightTopTileId != 12 && rightBottomTileId == 12) || (rightMiddleTileId == 12 && rightTopTileId == 12 && rightBottomTileId != 12))
{
blockedR = true;
}
else
{
blockedR = false;
}
//upBlock test
if((upMiddleTileId == 12 && upLeftTileId == 12 && upRightTileId == 12) || (upMiddleTileId == 12 && upLeftTileId != 12 && upRightTileId == 12) || (upMiddleTileId == 12 && upLeftTileId == 12 && upRightTileId != 12))
{
blockedU = true;
}
else
{
blockedU = false;
}
//downBlock test
if((downMiddleTileId == 12 && downLeftTileId == 12 && downRightTileId == 12) || (downMiddleTileId == 12 && downLeftTileId != 12 && downRightTileId == 12) || (downMiddleTileId == 12 && downLeftTileId == 12 && downRightTileId != 12))
{
blockedD = true;
}
else
{
blockedD = false;
}
#endregion
#region input capture
if(Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.S))
{
currentAnimation.Play("Run_Up");
if(!blockedU)
{
transform.Translate(0,((iSpeed * Time.deltaTime)+0.1f),0);
}
wasWDown = true;
wasADown = false;
wasSDown = false;
wasDDown = false;
isRunning = true;
}
if(Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.D))
{
currentAnimation.Play("Run_Left");
currentAnimation.SetFrame(0);
if(!blockedL)
{
transform.Translate(-((iSpeed * Time.deltaTime) + 0.1f),0,0);
}
wasWDown = false;
wasADown = true;
wasSDown = false;
wasDDown = false;
isRunning = true;
}
if(Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.W))
{
currentAnimation.Play("Run_Down");
if(!blockedD)
{
transform.Translate(0,-((iSpeed * Time.deltaTime) + 0.1f),0);
}
wasWDown = false;
wasADown = false;
wasSDown = true;
wasDDown = false;
isRunning = true;
}
if(Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.A))
{
currentAnimation.Play("Run_Right");
if(!blockedR)
{
transform.Translate(((iSpeed * Time.deltaTime) + 0.1f),0,0);
}
wasWDown = false;
wasADown = false;
wasSDown = false;
wasDDown = true;
isRunning = true;
}
if(Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.D))
{
currentAnimation.Play("Run_LeftDown");
if(!blockedD)
{
transform.Translate(0,-((iSpeed * Time.deltaTime) + 0.1f),0);
}
if(!blockedL)
{
transform.Translate(-((iSpeed * Time.deltaTime) + 0.1f),0,0);
}
wasWDown = false;
wasADown = true;
wasSDown = true;
wasDDown = false;
isRunning = true;
}
if(Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.A))
{
currentAnimation.Play("Run_RightDown");
if(!blockedD)
{
transform.Translate(0,-((iSpeed * Time.deltaTime) + 0.1f),0);
}
if(!blockedR)
{
transform.Translate(((iSpeed * Time.deltaTime) + 0.1f),0,0);
}
wasWDown = false;
wasADown = false;
wasSDown = true;
wasDDown = true;
isRunning = true;
}
if(Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.D))
{
currentAnimation.Play("Run_LeftUp");
if(!blockedU)
{
transform.Translate(0,((iSpeed * Time.deltaTime) + 0.1f),0);
}
if(!blockedL)
{
transform.Translate(-((iSpeed * Time.deltaTime) + 0.1f),0,0);
}
wasWDown = true;
wasADown = true;
wasSDown = false;
wasDDown = false;
isRunning = true;
}
if(Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.A))
{
currentAnimation.Play("Run_RightUp");
if(!blockedU)
{
transform.Translate(0,((iSpeed * Time.deltaTime) + 0.1f),0);
}
if(!blockedR)
{
transform.Translate(((iSpeed * Time.deltaTime) + 0.1f),0,0);
}
wasWDown = true;
wasADown = false;
wasSDown = false;
wasDDown = true;
isRunning = true;
}
if(isRunning)
{
if(!Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S))
{
isRunning = false;
}
}
else
{
if(wasWDown && !wasADown && !wasDDown && !wasSDown)
{
currentAnimation.Play("Idle_Up");
currentAnimation.SetFrame(0);
}
if(wasADown && !wasWDown && !wasSDown && !wasDDown)
{
currentAnimation.Play("Idle_Left");
currentAnimation.SetFrame(0);
}
if(wasDDown && !wasWDown && !wasSDown && !wasADown)
{
currentAnimation.Play("Idle_Right");
currentAnimation.SetFrame(0);
}
if(wasWDown && wasADown && !wasDDown && !wasSDown)
{
currentAnimation.Play("Idle_LeftUp");
currentAnimation.SetFrame(0);
}
if(wasWDown && !wasADown && wasDDown && !wasSDown)
{
currentAnimation.Play("Idle_RightUp");
currentAnimation.SetFrame(0);
}
if(wasSDown && !wasADown && !wasDDown && !wasWDown)
{
currentAnimation.Play("Idle_Down");
currentAnimation.SetFrame(0);
}
if(wasSDown && wasADown && !wasDDown && !wasWDown)
{
currentAnimation.Play("Idle_LeftDown");
currentAnimation.SetFrame(0);
}
if(wasSDown && !wasADown && wasDDown && !wasWDown)
{
currentAnimation.Play("Idle_RightDown");
currentAnimation.SetFrame(0);
}
}
#endregion
#endregion
}
}