Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by C3LT · Jul 11, 2012 at 11:13 PM · ragdollmeshcolliderhingejointconvex

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 :)

  1. 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.

  2. 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.

  3. 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:

alt text

Thanks in advance.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image C3LT · Jul 12, 2012 at 10:56 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Eric5h5 · Jul 12, 2012 at 08:33 AM

  1. 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.

    1. 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).

    2. According to the docs for Joint.anchor, the position is local space.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image C3LT · Jul 12, 2012 at 07:33 AM 0
Share

I'm still getting the error "'convex' is not a member of 'UnityEngineCollider'". Any ideas?

avatar image Eric5h5 · Jul 12, 2012 at 08:26 AM 0
Share

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;

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mesh Collider doesn't follow the mesh's triangles 1 Answer

Couldn't create a Convex Mesh from source mesh "car_1_body" within the maximum polygons limit (256). The partial hull will be used. Consider simplifying your mesh. 0 Answers

Create hollow sphere with objects bouncing around inside 2 Answers

Convex NonTrigger Mesh Collider + Rigidbody = weird transform position 0 Answers

Mesh collider does not move with ragdoll 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges