The question is answered, right answer was accepted
Is it possible to control prevent/enable colisions based on the value of a variable.
I'm currently still working on something 2d, which involves a sort of height system.
I want to know if it is possible to make it so that if the player attempts moves into ground with a height value too much higher than their own height value, they would be blocked from entry. But only if their height variable is not high enough.
EG. Player hieght: 1, Ground Height: 3 = no entry.
Player height 2, Ground height: 3 = entry allowed.
Any ideas?
Answer by jijigri · Jun 02, 2019 at 02:18 PM
I guess the simplest way would be to do this with some if/else statements. You create a script that you assign to your grounds, which takes a reference to the player, and to the collider. (Or you could get the player height variable by making it a static, but it's safer to get the player directly in the script.) Then you check if the player height is higher or equal to 3. If it is, you enable the collider, else you disable it. It should look like something like this: Private Collider2D groundCol; Private GameObject Player; Private float playerHeight;
Void Start() {
groundCol = GetComponent<BoxCollider2D>();
Player = GameObject.FindWithTag("Player"); //Finding with tag isn't the best option here, you might want to make the Player public, and directly drag its prefab in. Try what's best for your game.
playerHeight = Player.gameObject.GetComponent<PlayerScriptWhereTheHeightVariableIs>().heightVariable;
//If the player height changes, you should put the line above in the update function.
}
Void Update() {
if(playerHeight >= 4)
groundCol.enable = true;
else
groundCol.enable = false;
}
Being on mobile right now, I think you'll have to tweak the code a bit for it to work, but the system should work
Answer by CobbledGames · Jun 03, 2019 at 05:42 PM
Ok I've received your insight, I'll be trying to implement what I can based on it. Thank you.
Yeah I've gotten a solution. I deviated a bit from the original method but this will help. Tanks again.