So I’m trying to create an editor script that I can have on a lot of cubes to get them to create fixed joints from a raycast collision. That part works fine.
What I need now is to get it to be able to reapply the whole script with a button push or something. Right now I have to //comment out the @script ExecuteInEditMode(), save, try to reset the script on one of the cubes, go back, uncomment / save / reset the script on one of the cubes and it usually applies to all of them. Sometimes it only applies to that one cube. Any ideas?
Also at the bottom you’ll notice a script to remove the joints. Ive been trying to setup this GetComponents loop with a destroy command with no success. : / The current code destroys one joint at a time.
@script ExecuteInEditMode()
var done : boolean = false;
var rayRange : float = 4;
var jointBreakForce : int = 20;
var jointBreakTorque : int = 10;
function Update () {
var hit : RaycastHit;
var distanceToGround = hit.distance;
var checkJoint;
if (!done){
//Up
if (Physics.Raycast (transform.position, Vector3.up, hit, rayRange)) {
Debug.DrawLine (transform.position, hit.point);
checkJoint = hit.collider.gameObject.GetComponent(FixedJoint);
if (!checkJoint) {
var jointUp = gameObject.AddComponent(FixedJoint);
jointUp.connectedBody = hit.rigidbody;
jointUp.breakForce = jointBreakForce;
jointUp.breakTorque = jointBreakTorque;
}
}
done = true;
}
}
---------------------------------------
/*
@script ExecuteInEditMode()
var removeJoint : boolean = false;
function Update () {
if (!removeJoint){
DestroyImmediate(transform.gameObject.GetComponent(FixedJoint));
}
}
*/
That is not an editor script!
ExecuteInEditMore is a little helper to get the same behaviour in the editor as ingame. If you use stuff from the UnityEditor namespace in there you can’t even build your game.
I would suggest you write a real editor script and execute your desired actions either with a button inside an editor window or use a menu command.
I would attach a simple script to the cubes/objects on which you want to perform this task. (Maybe rayRange should be a constant in the editor script… would make more sense…)
// CubeScript.js
var rayRange : float = 4;
var jointBreakForce : int = 20;
var jointBreakTorque : int = 10;
This CubeScript is a normal script so it can be attached to a GameObject.
The following script is an editor script that should be placed in a subfolder that is called “editor”. The content of editor folders is not included in your build. It’s just available in the editor. If you place this script in your editor folder you will have a new submenu in Unity called “Cube Tools”. In this submenu you will find a menuitem “Create Cube Joints”. When you click this item you will execute CreateJoints()
.
This script searches for all CubeScripts in the scene and performs your raycast thing. I’ve altered the check so one cube can be connected to multiple objects. I just check if the current object is already jointed to another object but it doesn’t detect when another object is jointed to the current.
class CreateCubeJoint extends Editor
{
@MenuItem("Cube Tools/Create Cube Joints")
static function CreateJoints()
{
var cubes : CubeScript[] = GameObject.FindObjectsOfType(CubeScript);
for (var cube in cubes)
{
var joints = cube.GetComponents.<FixedJoint>();
var hit : RaycastHit;
if (Physics.Raycast (cube.transform.position, Vector3.up, hit, cube.rayRange))
{
var isJointed = false;
for (var joint in joints)
if (joint.connectedBody == hit.rigidbody)
isJointed = true;
if (isJointed == false)
{
var newJoint = cube.gameObject.AddComponent.<FixedJoint>();
newJoint.connectedBody = hit.rigidbody;
newJoint.breakForce = cube.jointBreakForce;
newJoint.breakTorque = cube.jointBreakTorque;
}
}
}
}
}