- Home /
Check object for component and connection?
So I made a script that does some raycasts in a few directions and when the rays collide with an object within range, it creates a FixedJoint on the current transform object and connects it to the ray collider.
All I need it to do now is check the collider to see if that collider already has FixedJoints on it and then check if one of those fixed joints is already connected to the current transform object (the object this script is on). If the connection doesn't exist, it creates one, and if it already does it does nothing. I seem to be stuck with it. Any help would be appreciated!
@script ExecuteInEditMode()
var done : boolean = false;
var rayRange : float = 5;
var jointBreakForce : int = 20;
var jointBreakTorque : int = 20;
function Update () {
var hit : RaycastHit;
var distanceToGround = hit.distance;
if (!done){
//Up
if (Physics.Raycast (transform.position, Vector3.up, hit, rayRange)) {
Debug.DrawLine (transform.position, hit.point);
var jointUp = gameObject.AddComponent(FixedJoint);
jointUp.connectedBody = hit.rigidbody;
jointUp.breakForce = jointBreakForce;
jointUp.breakTorque = jointBreakTorque;
}
//Down
if (Physics.Raycast (transform.position, -Vector3.up, hit, rayRange)) {
Debug.DrawLine (transform.position, hit.point);
var jointDown = gameObject.AddComponent(FixedJoint);
jointDown.connectedBody = hit.rigidbody;
jointDown.breakForce = jointBreakForce;
jointDown.breakTorque = jointBreakTorque;
}
done = true;
}
}
Answer by save · May 21, 2011 at 07:39 AM
You could check it with something like this:
if (Physics.Raycast (transform.position, Vector3.up, hit, rayRange)) {
var checkJoint = hit.gameObject.GetComponent(FixedJoint);
if (!checkJoint) {
//carry on with attachment here
}
}
And lift out the var declaration of checkJoint to set it at the top of your script. If you have more directions later perhaps a for-loop would server better to not make your script so long.
Your answer