- Home /
How to assign a joint's 'connected body'?
I have a lot of objects in my scene that need to be connected together using joints, I was wondering if anyone has come across an easier way to assign a joint's connected body than by dragging and dropping each individual object to each individual joint in the inspector?
It would be really handy if, for instance: Unity could assign a joint's connected body to any object that is intersecting with that joint's parent object in the scene view.
Has anybody come across anything like this?
Or would anyone even know the best way to go about assigning hundreds of unique objects to hundreds of separate joints efficiently?
Answer by FortisVenaliter · Jan 27, 2016 at 02:50 PM
No, there's nothing built in like that that I know of, but it wouldn't be hard to write a script to do so. Just make sure you enable "ExecuteInEditMode" on the script, and have it search for all objects with a specific name or tag, then compare their positions to other objects and link it all up dynamically.
Wow nice one! I didn't realize it was possible to run scripts outside of play mode!
Would you be able to point me in the right direction for learning how to do this? I understand what your saying, in theory, it's just I can't seem to figure out the right syntax or logic something. The Unity documentation on "ExecuteInPlay$$anonymous$$ode" doesn't seem to tell me a lot about how to write scripts that will run outside of play mode. Would you know of anywhere where I could find more information on this?
I have written the following script, and it works in playmode, it assigns a rigidbody on game object (a) to a fixed joint on game object (b) when the two collide:
using UnityEngine;
using System.Collections;
[ExecuteInEdit$$anonymous$$ode]
public class ConnectedBodyAuto : $$anonymous$$onoBehaviour {
private FixedJoint BlazeIt;
private Rigidbody targetJoint;
// Use this for initialization
/* void Start () {
BlazeIt = gameObject.GetComponent<FixedJoint>();
}*/
// Update is called once per frame
void Update () {
BlazeIt = gameObject.GetComponent<FixedJoint>();
}
void OnCollisionEnter (Collision col)
{
if(col.gameObject.name == "Cube")
{
Debug.Log ("hit!");
targetJoint = col.rigidbody;
Debug.Log (targetJoint);
BlazeIt.connectedBody = targetJoint;
Debug.Log (BlazeIt.connectedBody);
}
}
}
//when something collides with this cube, put that object in the connected body variable of the fixed joint
But when I stuck [ExecuteInEdit$$anonymous$$ode] on the script like so, it gives me no errors, but doesn't seem to do much either. Do collisions register outside of play mode? Am I going about this totally wrong?
Collisions do not register in edit mode. You're going to need to take a distance-based approach.
Your answer
Follow this Question
Related Questions
Can you get a visualization of joint limits in the editor as you set them up? 0 Answers
Is disable google analytics in editor pro only or does it work with plus too? 0 Answers
Hammer Swing - Joint Break Force and Hammer Object 1 Answer
Can I reuse editor extension assets for multiple projects/games? (Accelerator Pack) 1 Answer
How can i create a pup-up menu like the one for choose the shader on materials in unity 2019? 1 Answer