- Home /
Box2D empty gameObjects not getting expected results code is included
See screen for explanation: your the blue square and you press a key and you move down. if you hit the block you should stop.
However if you hit the top of the green block with your bottom you shouldn't be able to go any further toward the bottom
I created my own playercontrol sort of like what Unity training videos has with their CharacterController2D and i have empty gameobjects to the top left bottom and right of the box
and here is the provided code below
using UnityEngine;
using System.Collections;
public class PlayerMove : MonoBehaviour {
public float startingSpeed = 5;
public bool moveLeft { get; set; }
public bool moveRight { get; set; }
public bool moveUp { get; set; }
public bool moveDown { get; set; }
public bool dontMove { get; set; }
public float Speed { get; set; }
public bool isGroundedBottom = false;
public bool isGroundedLeft = false;
Transform TopCheck;
Transform LeftCheck;
Transform BottomCheck;
Transform RightCheck;
[SerializeField] LayerMask whatIsGround;
float Radius = .2f;
void FixedUpdate(){
/*if (Physics2D.OverlapCircle (new Vector2(BottomCheck.position.x, BottomCheck.position.y), Radius, whatIsGround)) {
moveDown = false;
}
if (Physics2D.OverlapCircle (new Vector2(LeftCheck.position.x, LeftCheck.position.y), Radius, whatIsGround)) {
moveLeft = false;
}
if(Physics2D.OverlapCircle(new Vector2(RightCheck.position.x, RightCheck.position.y), Radius, whatIsGround)){
moveRight = false;
}
if(Physics2D.OverlapCircle(new Vector2(TopCheck.position.x, TopCheck.position.y), Radius, whatIsGround)){
moveUp = false;
}*/
}
void Awake(){
TopCheck = transform.Find ("TopCheck");
LeftCheck = transform.Find ("LeftCheck");
BottomCheck = transform.Find ("BottomCheck");
RightCheck = transform.Find ("RightCheck");
}
// Use this for initialization
void Start () {
Speed = startingSpeed;
}
void OnTriggerEnter2D(Collider2D other)
{
DontMove ();
Debug.Log ("TEST HIT");
Debug.Log ("Game Object Name" + other.name.ToString () + "Tag" + other.tag.ToString () + "Other.Gameobject" + other.gameObject.name.ToString () + "other gameObject Tag" + other.gameObject.tag.ToString());
}
void OnTriggerExit2D(Collider2D other)
{
Debug.Log ("Game Object Name" + other.name.ToString () + "Tag" + other.tag.ToString () + "Other.Gameobject" + other.gameObject.name.ToString () + "other gameObject Tag" + other.gameObject.tag.ToString());
}
// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.Space)) {
dontMove = true;
moveLeft = false;
moveRight = false;
moveUp = false;
moveDown = false;
}
if ((Input.GetAxis ("Horizontal") < -.1) || Input.GetKey (KeyCode.LeftArrow)) {
MoveLeft ();
}
if ((Input.GetAxis ("Horizontal") > .1) || Input.GetKey (KeyCode.RightArrow)) {
MoveRight ();
}
if ((Input.GetAxis ("Vertical") > .1) || Input.GetKey (KeyCode.UpArrow)) {
MoveUp ();
}
if ((Input.GetAxis ("Vertical") < - .1) || Input.GetKey (KeyCode.DownArrow)) {
MoveDown ();
}
//move player according to which direction they can move
if (moveLeft) {
this.transform.Translate ((-1) * Speed * Time.deltaTime, 0f, 0f);
} else if (moveRight) {
this.transform.Translate (Speed * Time.deltaTime, 0f, 0f);
} else if (moveUp) {
this.transform.Translate (0f, Speed * Time.deltaTime, 0f);
} else if (moveDown) {
this.transform.Translate (0f, (-1) * Speed * Time.deltaTime, 0f);
} else if (dontMove) {
this.transform.Translate (0f, 0f, 0f);
}
//check to see if we go off the screen then reset
if (this.transform.position.x <= -10) {
StopIt ();
this.transform.position = new Vector3 (0f, 0f, 0f);
} else if (this.transform.position.x >= 200) {
StopIt ();
this.transform.position = new Vector3 (0f, 0f, 0f);
} else if (this.transform.position.y <= -10) {
StopIt ();
this.transform.position = new Vector3 (0f, 0f, 0f);
} else if (this.transform.position.y >= 200) {
StopIt ();
this.transform.position = new Vector3 (0f, 0f, 0f);
}
//check collision on update
}
private void StopIt()
{
dontMove = true;
moveUp = false;
moveLeft = false;
moveRight = false;
moveDown = false;
}
private void MoveLeft()
{
moveLeft = true;
moveRight = false;
moveUp = false;
moveDown = false;
}
private void MoveRight()
{
moveRight = true;
moveLeft = false;
moveDown = false;
moveUp = false;
}
private void MoveDown()
{
moveDown = true;
moveLeft = false;
moveRight = false;
moveUp = false;
}
private void MoveUp()
{
moveUp = true;
moveLeft = false;
moveRight = false;
moveDown = true;
}
private void DontMove()
{
dontMove = true;
moveDown = false;
moveLeft = false;
moveRight = false;
moveUp = false;
}
}

I've commented out FixUpdate since i thought my empty gameobjects i'm trying to check collision with much like how the sample prefab 2d robot in the asset store checks for ground and ceiling i made sure the were nested under the player prefab and that and the players sprite was under layer, "Player" but whenever i added the FixUpdate it moved 5 pixels and would stop move 5 pixels and stop
What I am looking for in this example as i'm learning unity and i'm a noob is press a key move in direction until you collide bottom of your sprite to the top if you move down and you collide against top sprite you can't move down
I can't figure this out for some reason guess i'm just a dumb noob :(
PLEASE HELP!!!!
Your answer