• Advertisement
Sign in to follow this  

Disable Colliders

This topic is 906 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, so I'm new to game developing and I have essentially no coding experience aside from a few YouTube tutorials. I'm trying to make something that when the play clicks a button, it'll deactivate the collider a allow the player to walk through it.

 

using UnityEngine;
using System.Collections;
 
public class DisableCollider : MonoBehaviour
     {
          public int x;
          void Update (){
               if (x == 0) {
               if (Input.GetKey (KeyCode.E)) {
                    GetComponent<Collider> ().enabled = false;
                    x = 1;
               }
          }
     }
}
 
I also wanted to press the same button to reactivate the collider as well. Any help?

 

Share this post


Link to post
Share on other sites
Advertisement

Reactivating it would be as simple as setting the collides enabled variable to true. The way you have done it if x = 0 you want the door to be closed, and if x is one you want it to be open. Intsead of using ints for this I Highly recommend you use Booleans. Since the door only has two states, opened and closed it would better to have a true / false. Your checking every update to see if the door is open, then you check to see if the player is pressing E. You also need to do the opposite, which is checking to see if the door is opened and if the players presses e, then you enable the collider

Share this post


Link to post
Share on other sites
Do you mean something like this?
public class DisableCollider : MonoBehaviour
{
	public bool x;
	void Update (){
		if (x == true) {
			if (Input.GetKey (KeyCode.E)) {
				GetComponent<Collider> ().enabled = false;
				x = false;
			}
		}
		if (x == false) {
			if (Input.GetKey (KeyCode.E)) {
				GetComponent<Collider> ().enabled = true;
				x = true;
			}
		}
	}
}

I tested it out, and not working for me. It won't change the state of the collider or x.

Share this post


Link to post
Share on other sites

x was only temp, but thank you very much, and yes it is. It is on a wall in a house, and I want the player to be able to walk to the wall and press E so that they can walk through it.

Share this post


Link to post
Share on other sites

I don't know if you've figured it out, but whats happening is it's disabling it and instantly renabling it, this is because the way your using If Statements. Using an else if as the second

statement should fix your problem!

Share this post


Link to post
Share on other sites

Thank you very much, it got the Open to change states, but not the Box Collider. Also, it seems to very inconsistent.

Edited by forgetful_user

Share this post


Link to post
Share on other sites
public class DisableCollider : MonoBehaviour
{
	Collider myCollider;
	
	void Start() {
		myCollider = GetComponent<Collider>();
	}
  
	void Update (){
		if(Input.GetKeyDown(KeyCode.E)) {
			myCollider.enabled = !myCollider.enabled;
		}
	}
}

When you're using GetComponent<> to fetch something, consider fetching it once in Start() and storing it to avoid the cost of fetching it every frame. Input.GetKey() is true as long as the key is held, but Input.GetKeyDown() is only true on the frame that the key was first pressed, so you can use it to make things happen only once per key-press.

Share this post


Link to post
Share on other sites

Here's the result, still won't interact with the Box Collider.

public class DisableCollider : MonoBehaviour
{
	Collider boxCollider;

	public bool Open;

	void Start() {
		boxCollider = GetComponent<Collider>();
	}

	void Update (){
		if (Open == true) {
			if(Input.GetKeyDown(KeyCode.E)) {
				boxCollider.enabled = !boxCollider.enabled;
				Open = false;
			}
		}
		else if (Open == false) {
			if(Input.GetKeyDown(KeyCode.E)) {
				boxCollider.enabled = !boxCollider.enabled;
				Open = true;
			}
		}
	}
}

Share this post


Link to post
Share on other sites

To see if its a problem with the fetching the object or not make it a [Serilizable] private or public and select the collider in the scene. Then run the code and see if it works, or to see if its something else wrong with the problem

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement