- Home /
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.
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.
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.
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.
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.
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))