Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 VoLkizz · Oct 20, 2014 at 09:34 PM ·

How to make threshold between prefabs?

Hi, I have cubes in the scene that is prefabs. They have script on them that making them push/pullable. I want to setup threshold between them so they stop moving when too close to each others. I know how to make it with two different objects but not with a prefab. Using C# language. Thanks in advance.

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 KpjComp · Oct 20, 2014 at 09:40 PM 0
Share

Hi, it might help if you show your code you have done for different objects, and then we could maybe explain how to change for prefabs.

avatar image VoLkizz · Oct 20, 2014 at 11:34 PM 0
Share

here it is:

    public float threshold = 1.5f;
     public Transform boxT;
     public GameObject box;
     
        void Start()
          {
            box = GameObject.Find ("Cube");
            boxT = box.transform;
          }
        void FixedUpdate()
          {
            if ((gameObject.transform.position - boxT.transform.position).magnitude < threshold )
              {
              EnableCollider();
              }
             else if ((gameObject.transform.position - boxT.transform.position).magnitude > threshold )
              {
             DisableCollider();
              }
          }
 
 
       void EnableCollider()
      {
        BoxCollider collider1 = gameObject.transform.GetComponentInChildren<BoxCollider>();
              if(collider.name == "EmptyCollider")
                 {
            collider.enabled = true;
                 }
          }
  void DisableCollider()
      {
        BoxCollider collider1 = gameObject.transform.GetComponentInChildren<BoxCollider>();
              if(collider.name == "EmptyCollider")
                 {
            collider.enabled = false;
                 }
          }

     

// need to make it work for multiple objects that carrying the same script.

avatar image VoLkizz · Oct 20, 2014 at 11:59 PM 0
Share

the idea is the cube has two different colliders. One is on it and taking half of cube size other is on empty children that taking other half (charside) from where character can push/pull it. So when charside is disable you can push/pull, but when it's enable it prevent Character Controller from moving forward(but you still can pull it back). This way I see less mess in the scene but don't know how to make prefabs like that.

avatar image KpjComp · Oct 21, 2014 at 09:21 AM 0
Share

Ok, I think I understand what your after, must admit read a few times so not 100% sure what your trying to do. From what I can tell with you using "(gameObject.transform.position - boxT.transform.position).magnitude", this is basically what a Collider is for, but in this case it would be Sphere Collider rather than box. If you use a Collider here you could then check it's OnCollisionEnter & OnCollisionExit to enable/disable your other Collider.

avatar image VoLkizz · Oct 21, 2014 at 05:35 PM 0
Share

Yep, you're right. it working much better with OnCollisionEnter/Exit. Thanks for advice. P.S. previous stuff (with magnitude) i found on the forum so just tried as it is))

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

timer that continues through all scences 3 Answers

How do I convert pixel colours to numbers? 2 Answers

how to disable specific axis in input manager thru the script 1 Answer

How do i call a function from another script in another object 1 Answer

How do i change my background for android gaming 1 Answer


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