- Home /
Setting meshCollider.convex = true via script
Hi, I am building a ragdoll character and I am using this script to initialise all the body parts:
var manParentBone:GameObject;
var manJointObject:GameObject;
var manBoneWeight:float;
@HideInInspector //hides var below from the inspector
var manInitialiseJoints = true;
function Start () {
gameObject.AddComponent ("Rigidbody");
gameObject.AddComponent ("MeshCollider");
gameObject.AddComponent ("HingeJoint");
}
function Update () {
if(manInitialiseJoints == true)
{
rigidbody.isKinematic = false;
rigidbody.mass = manBoneWeight;
meshCollider.convex = true;
hingeJoint.connectedBody = manParentBone.rigidbody;
hingeJoint.anchor = manJointObject.transform.position;
manInitialiseJoints = false;
}
}
This is my first project in Unity (it's more of a physics experiment than a game) and I've run into a few problems - I've probably bitten off far more than I can chew :)
I had a problem where I wasn't able to set the properties of the components in the Start() function because they didn't exist yet - so I moved them into the first tick of the Update() function. Is that the right way to do it? Seems to work OK.
The meshCollider line doesn't seem to work and says meshCollider is not a property. How do I set this in the script so that I can set it to be a convex mesh? I know it should work because I've manually added the mesh collider component and it's fine.
I set the hinge joint anchor position by using the position of another object (I position a small sphere at each joint("HingeJoint") to use as a reference point for the hinge). The position that gets put into the hinge anchor doesn't appear to be the correct values - they're way too high. Do I need to specify it's the world space position somehow?
Here's my workspace:
Thanks in advance.
Hi, I'm having a further problem with this script - I can't get the hingejoint parameters setting correctly. It refuses to assign the connectedbody, anchor position etc. I've tried putting it in the update() function and it see$$anonymous$$gly works fine.
var manParentBone:GameObject;
var manJointObject:GameObject;
var manBoneWeight:float;
var manIsEndBone:boolean;
@HideInInspector //hides var below from the inspector
var testbool:boolean = true;
function Start () {
if (manBoneWeight==0) // check to make sure the bone has weight
{
Debug.Log (gameObject.name+" has zero weight, setting to 1.");
manBoneWeight=1;
}
var rb=gameObject.AddComponent (Rigidbody);
var mc=gameObject.AddComponent ($$anonymous$$eshCollider);
rb.mass = manBoneWeight;
mc.convex = true;
if (manIsEndBone==false) // Does the bone have parents? If so, make a hinge connection to the parent.
{
var hj=gameObject.AddComponent (HingeJoint); //create hinge
hj.connectedBody = manParentBone.rigidbody; //connect hinge
hj.anchor = manJointObject.transform.position-gameObject.transform.position;
hj.useLimits = true; //use hinge limits
hj.limits.$$anonymous$$ = 5;
hj.limits.max = -5;
}
}
function Update () {
}
As well as not being able to set the anchor inside the start() function, when I do set the anchor it seems to be wrong. If I want to set the position of the anchor to the manJointObject, then I just $$anonymous$$us the manJointObject position from the bone. This should give me the position of the joint relative to the bone, right?
Thanks in advance for your help, I really appreciate it.
Answer by Eric5h5 · Jul 12, 2012 at 08:33 AM
No, Update runs every frame; it should not be used like that for something that only happens once. The components do exist in Start as soon as you add them, so you can add your code below those AddComponent lines.
You can see in the docs for GameObject that there is no shortcut for MeshCollider, so you have to use GetComponent, or else store the reference when using AddComponent (as shown in the other answer).
According to the docs for Joint.anchor, the position is local space.
Answer by OperationDogBird · Jul 12, 2012 at 12:28 AM
You can set the values in Start once you create them, for ease of use i create a var for each new component for easy access and to avoid redundant lengthy component calls.
function Start () {
var rb=gameObject.AddComponent (Rigidbody);
var mc=gameObject.AddComponent (MeshCollider);
var hj=gameObject.AddComponent (HingeJoint);
rb.isKinematic = false; //No reason for this, it defaults to false
rb.mass = manBoneWeight;
mc.convex = true;
hj.connectedBody = manParentBone.rigidbody;
hj.anchor = manJointObject.transform.position;
}
Not sure on ur placements of the hinge joints however.
I'm still getting the error "'convex' is not a member of 'UnityEngineCollider'". Any ideas?
AddComponent should not use quotes; that makes it return Component ins$$anonymous$$d of the actual component type being added.
var mc = gameObject.AddComponent ($$anonymous$$eshCollider);
mc.convex = true;