Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Abudhabi12 · Feb 02, 2019 at 07:24 AM · unity 5collisioncollidergamespacman

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.

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SteenPetersen · Feb 02, 2019 at 07:48 AM 0
Share

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

avatar image Abudhabi12 SteenPetersen · Feb 02, 2019 at 07:58 AM 0
Share

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);
         
        
        
         
         
        }
 }

avatar image Abudhabi12 Abudhabi12 · Feb 02, 2019 at 07:59 AM 0
Share

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()
 {
     
 }

}

Show more comments
avatar image Abudhabi12 SteenPetersen · Feb 02, 2019 at 08:00 AM 0
Share

And if use the collider.enabled false then the ghost wont be able to eat me if youre familar with pacman

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

247 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to decrease the speed of the player by some fraction after triggering the box collider? 2 Answers

Can you help me about collision please ? 3 Answers

Box and circle Collider doesn't work 1 Answer

Check Object Is Not Colliding 2 Answers

Detecting collision of two game objects? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges