I would think this would be simple but for some reason, I can't figure this out. I have 4 child GameObjects around my player, each one with it's own collider. I need to be able to enable and disable the child GameObjects based on the direction the character is moving. So if the player is moving right the left, top and bottom colliders will be disabled leaving just the right collider active. If the Player moves up then the Left, Right, and Bottom colliders will be disabled leaving just the Top collider. And it's the same principle for the other 2 directions. But for some reason, my code works for both the left and right directions, but when my character moves up or down it disables everything except the left collider. Can I get some help? Here is my code:
public BasicNPCPrototype NPCPrototype;
private bool nearNPC = false;
public bool facingRight;
public bool facingUp;
public GameObject RightCollider;
public GameObject LeftCollider;
public GameObject TopCollider;
public GameObject BottomCollider;
// Use this for initialization
protected override void Start()
{
base.Start();
}
// Update is called once per frame
protected override void Update()
{
// Call the GetInput Function
GetInput();
// Call the CheckIfNear function
CheckIfNear();
// Call the CheckDirection function
CheckDirection();
// Call the DisableInteractionColliders function
DisableInteractionColliders();
base.Update();
}
void GetInput()
{
if(playerActive)
{
direction = Vector2.zero;
// Get the horizontal Axis and put in the X value of "direction"
direction.x = Input.GetAxisRaw("Horizontal");
// Get the Vertical Axis and put in the Y value of "direction"
direction.y = Input.GetAxisRaw("Vertical");
}
}
// Look at our movement direction and decide which way were facing
void CheckDirection()
{
if (direction.x > 0)
{
facingRight = true;
facingUp = false;
}
else if (direction.x < 0)
{
facingRight = false;
facingUp = false;
}
if (direction.y > 0) // && !facingRight)
{
facingUp = true;
facingRight = false;
}
else if (direction.y < 0) // && !facingRight)
{
facingUp = false;
facingRight = false;
}
}
// Look at what direction the Player is facing and disable/enable the correct colliders to account for it
void DisableInteractionColliders()
{
// WIP
if(facingRight)
{
RightCollider.SetActive(true);
LeftCollider.SetActive(false);
TopCollider.SetActive(false);
BottomCollider.SetActive(false);
} else if(!facingRight)
{
LeftCollider.SetActive(true);
RightCollider.SetActive(false);
TopCollider.SetActive(false);
BottomCollider.SetActive(false);
}
else if(facingUp)
{
TopCollider.SetActive(true);
RightCollider.SetActive(false);
LeftCollider.SetActive(false);
BottomCollider.SetActive(false);
} else if(!facingUp)
{
BottomCollider.SetActive(true);
RightCollider.SetActive(false);
LeftCollider.SetActive(false);
TopCollider.SetActive(false);
}
}
If it's easier to read you can read it over on PasteBin: https://pastebin.com/y1jQT17w
Ignore the CheckIfNear(); call in teh update function as that's for future plans but it currently empty.
if(facingRight)
{
RightCollider.SetActive(true);
LeftCollider.SetActive(false);
TopCollider.SetActive(false);
BottomCollider.SetActive(false);
}
else if(!facingRight)
{
LeftCollider.SetActive(true);
RightCollider.SetActive(false);
TopCollider.SetActive(false);
BottomCollider.SetActive(false);
}
else if(facingUp)
{
TopCollider.SetActive(true);
RightCollider.SetActive(false);
LeftCollider.SetActive(false);
BottomCollider.SetActive(false);
}
else if(!facingUp)
{
BottomCollider.SetActive(true);
RightCollider.SetActive(false);
LeftCollider.SetActive(false);
TopCollider.SetActive(false);
}
facingRight is either True or False. This means that your first pair of if statement (if(facingRight)
and if(!facingRight)
) is always true, so your code never reaches the 3rd if statement and your variable facingUp is never used.
If I were you I would assign used an integer to store the direction and select the colliders using a switch. Like this:
int dir = 0;
void CheckDirection()
{
if (direction.x > 0)
{
dir = 1;
}
else if (direction.x < 0)
{
dir = 2;
}
if (direction.y > 0)
{
dir = 3;
}
else if (direction.y < 0)
{
dir = 4;
}
}
.
void DisableInteractionColliders()
{
LeftCollider.SetActive(false);
RightCollider.SetActive(false);
TopCollider.SetActive(false);
BottomCollider.SetActive(false);
switch(dir)
{
case 1: RightCollider.SetActive(true);
break;
case 2: LeftCollider.SetActive(true);
break;
case 3: TopCollider.SetActive(true);
break;
case 4: BottomCollider.SetActive(true);
break;
default: break;
}
}