in Education by
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. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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; } }

Related questions

0 votes
    I would think this would be simple but for some reason, I can't figure this out. I have 4 child ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
0 votes
    Do I risk losing sales by disabling SSL 2.0 and PCT 1.0 in IIS5? Clarification: Sales would be ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    i have a UiBarButton item in my Toolbar.i need to deactivate the user touch interaction in UiBarButton. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    I posted this : PHP 5.2.x: $_POST is empty when any field has value of "drop anywords from ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    Auto-configuration report can be logged to the console by enabling debug mode while starting the application. A. True B. False...
asked Nov 8, 2022 in Education by JackTerrance
0 votes
    I have a relative layout that has an ImageView and and a textview aligned to right of the ImageView. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I have a relative layout that has an ImageView and and a textview aligned to right of the ImageView. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I have a relative layout that has an ImageView and and a textview aligned to right of the ImageView. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    What are cloud-enabling technologies?...
asked Aug 8, 2021 in Technology by JackTerrance
0 votes
    Which of the following options is cloud computing key enabling technologies? 1. Cloud APIs 2. Cloud Storage 3. Virtualization 4. All of the options 5. Cloud Data centers...
asked Apr 11, 2021 in Technology by JackTerrance
0 votes
    What is the command used for enabling Splunk to boot start?...
asked Oct 31, 2020 in Technology by JackTerrance
0 votes
    In magnetic disk ________ stores information on a sector magnetically as reversals of the direction of ... division Storage and File Structures of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
...