- Home /
Disabling and Enabling a collision
I am doing a pac man game in the frightened mode I want to stay in frightened mode for around 10 seconds but then the game will continue normally. So as you may all know in fright mode the pacman can kill the ghost and this was done but now when I am trying to go to the normal mode I cant switch it around so the ghost can eat the pacman. Once the ghost collides with the pacman they get eaten. Any ideas i tried to disable the script but still the collision is detected.
I would need to take a look at some of your code at how you are trying to perform this task. but to answer your title it is relatively simple to enable or disable colliders at runtime by simply typing
collider.enabled = false
you dont need to disable the entire gameobject. please show me some code.
https://docs.unity3d.com/ScriptReference/Collider-enabled.html
First Script so the enemy can eat the pacman and the pacman respawns somewhere else
void OnTriggerEnter2D(Collider2D coll){
if (coll.name == "pacman")
{
//LifeControl.health -=0.5;
Debug.Log(LifeControl.health);
player.transform.position = respawnPoint.transform.position;
//Destroy(coll.gameObject);
}
}
Second Script which will be enabled one I have the cherry eaten
GameObject
objblueenemy,objredeneemy,objyellowenemy,objvioletenemy,pacman;
// Start is called before the first frame update
void Start()
{
pacman = GameObject.Find("pacman");
objblueenemy = GameObject.Find("blueenemy");
objredeneemy = GameObject.Find("redenemy");
objyellowenemy = GameObject.Find("yellowenemy");
objvioletenemy = GameObject.Find("violetenemy");
}
void OnTriggerEnter2D(Collider2D coll){
if (coll.name == "blueenemy")
{
Enemy$$anonymous$$illsPacman simple = objblueenemy.GetComponent<Enemy$$anonymous$$illsPacman>();
simple.respawnPoint.transform.position = objblueenemy.transform.position;
objblueenemy.SetActive(false);
Debug.Log("Blue killed");
//Destroy(objblueenemy);
}
if (coll.name == "redenemy")
{
Enemy$$anonymous$$illsPacman simple = objredeneemy.GetComponent<Enemy$$anonymous$$illsPacman>();
simple.respawnPoint.transform.position = objredeneemy.transform.position;
objredeneemy.SetActive(false);
Debug.Log("red killed");
//Destroy(objblueenemy);
}
if (coll.name == "yellowenemy")
{
Enemy$$anonymous$$illsPacman simple = objyellowenemy.GetComponent<Enemy$$anonymous$$illsPacman>();
simple.respawnPoint.transform.position = objyellowenemy.transform.position;
objyellowenemy.SetActive(false);
Debug.Log("yellow killed");
//Destroy(objblueenemy);
}
if (coll.name == "violetenemy")
{
Enemy$$anonymous$$illsPacman simple = objvioletenemy.GetComponent<Enemy$$anonymous$$illsPacman>();
simple.respawnPoint.transform.position = objvioletenemy.transform.position;
objvioletenemy.SetActive(false);
Debug.Log("Violet killed");
//Destroy(objblueenemy);
}
}
// Update is called once per frame
void Update()
{
}
}
And if use the collider.enabled false then the ghost wont be able to eat me if youre familar with pacman
Answer by SteenPetersen · Feb 02, 2019 at 09:51 AM
I can see that you are using aron granbergs A* pathfinding system. thats cool its a good setup.
I think that what you should consider doing is to avoid this disabling and enabling of scripts depending on conditions. Instead you can consider just making an enum and a state for pacman:
All code down here is pseudoCode just so you can get an idea of a different approach
public enum State
{
Vulnerable,
PowerState,
Dead
}
then from there you can simply change the state of pacman depending on for example picking up a cherry:
Void PickedUpCherry()
{
pacman.currentState = State.PowerState;
}
once you have a system like that setup you can simply do conditionals in script as in:
if(pacman.currentstate == State.PowerState)
{
//Do stuff that happens when he is in this state
// Make him flash
// Run a loop that sets all enemies to State.Vulnerable
// etc.
}
Let me know if there is something you dont understand. I believe this to be an architecture that would be easier to work with and avoid switching scripts off. You can simply determine inside the triggers of your colliders what state they are in and act accordingly.
hope it helps.