- Home /
Make invisible wall appear visible upon collision/detection
SO I've managed to make a script in which when a visible wall collides with the player's bullet it will disable the renderer & collider for 6 seconds before reappearing. Now I want to make a secondary script which makes invisible walls with their colliders & renderers disabled and upon collision/detection of a bullet passing through, it enables both renderers & colliders for a specific set of time or until the player shoots the wall again to make it disappear.
The code for making the wall invisible is shown here:
function OnTriggerEnter(col : Collider) {
if (col.tag == "Bullet") { //If it is the bullet entering the collider
renderer.enabled = false;
collider.enabled = false;
Invoke ("ReEnable",5); //Re-enable it in 10 seconds
}
}
function ReEnable() {
renderer.enabled = true;
collider.enabled = true;
}
The main problem I've been encountering is that when the collider is disabled firstly when a bullet is shot pass it won't detect the collision to enable the collider & renderer as the collider isn't turned on in the first place.
Any ideas?
Never tried it dynamically, but can't you just set the 'isTrigger' flag rather than disable the collider?
http://docs.unity3d.com/ScriptReference/Collider-isTrigger.html
Answer by richyrich · Oct 23, 2014 at 04:18 PM
How about this...
function OnTriggerEnter(col : Collider)
{
if (col.tag == "Bullet")
{
//If it is the bullet entering the collider
if (!isOnCountdown)
{
renderer.enabled = false;
isOnCountdown = true;
Invoke("ReEnable", 5); //Re-enable it in 10 seconds
}
else
{
renderer.enabled = true;
}
}
}
function ReEnable()
{
renderer.enabled = true;
isOnCountdown = false;
}
isCountDown is a bool declared at the top of the script
Only problem I have with this is strangely enough the declaring of the bool :-D each time I declare it above function OnTriggerEnter as Boolean isOnCountdown; I get errors stating to add a semi-colon on the end when already is 1...
It should be
var isOnCountdown : boolean = false;
If you were to use c#, it would just be
bool isOnCountdown = false;
I appreciate you don't technically need the '= false part' in this case, but I find it easier to initialise all variables whatever they are to avoid nasty surprises later on.
Code works fine, only bug I've found is that when the player shoots bullet at it first time it does nothing, only on 2nd time bullet has been fired at it does it activate and also the collider doesn't enable itself to stop the player walking through it.
Code works fine, only bug I've found is that when the player shoots bullet at it first time it does nothing, only on 2nd time bullet has been fired at it does it activate and also the collider doesn't enable itself to stop the player walking through it.
The collider should never be disabled in the first place. When the program starts, the collider should be enabled and stay that way for the duration of the game, unless you have some other feature you are implementing which interferes.
As for the player walking through, your trigger collider is not designed for that purpose. If you want that behaviour, you could just put another collider in the same location (but don't tick isTrigger) and set it as enabled or not depending on whether the renderer is enabled or not.
Your answer
Follow this Question
Related Questions
collision wont work 1 Answer
Collision not working 3 Answers
Detecting Trigger Collision for Mecanim Animator 0 Answers
Collision detection not working properly with 2D sprites 1 Answer
Spawning and health script not working with collider? 1 Answer