- Home /
Collision between objects?
I'm currently doing collisions using the following code:
function OnTriggerEnter (boxCollider : Collider) {
if(boxCollider.collider.name == "Box"){
hittingVar = true;
}
}
This is added to an object (A) and detects collisions with another object (B). However now what I want to do is to look at collisions between A and B from a third object (C). This needs to be extremely precise and up to date so basically I'm wondering if there is a piece of code I could use to say "If A intersects B then do this" that I could apply to C. I don't want to simply use variables and get them from another script because as I say it needs to be extremely up to date and there seems to be a small amount of lag when i do that :).
Thanks in advance!
Answer by Wolfram · Aug 04, 2010 at 05:04 PM
There should be no lag unless you search object C within your OnTriggerEnter() function everytime. Instead, search your object C in Awake() or Start() once (or directly assign it in the Inspector), and then refer to that variable. Then modify your script to call one of C's functions:
var objC : ObjectCScript;
function Awake () { objC = Find("ObjectC's name").GetComponent(ObjectCScript); }
function OnTriggerEnter (boxCollider : Collider) { if(boxCollider.collider.name == "Box"){ objC.DetectedCollision(); } }
Answer by Extrakun · Aug 04, 2010 at 05:03 PM
A couple of options.
You can set a reference to your third object
public subject:ComponentC
function OnTriggerEnter (boxCollider : Collider) { if(boxCollider.collider.name == "Box"){ hittingVar = true; subject.NotifyCollision(this.gameObject, boxCollider.gameObject) }
}
You can use send message
function OnTriggerEnter (boxCollider : Collider) {
if(boxCollider.collider.name == "Box"){
hittingVar = true;
GameObject.FindWithTag("Subject").SendMessage("NotifyCollision");
}
Setting a variable directly on C should be faster than any of those method, IMHO, but ugly and prone to errors.