- Home /
rotate bones before bindposes
please a need help with this.
I have used this code http://unity3d.com/support/documentation/ScriptReference/Mesh-bindposes.html, modify but I noticed that the bones did not rotate. I've tried in this way to see the bones:
void Update(){
try{
for ( int i = 0; i < bones.Length; i++ ){
Debug.DrawLine (bones[i].transform.position,bones[i].parent.transform.position, Color.green);
}
}catch(Exception e){}
}
and this way to rorate:
bones[1] = new GameObject("Upper").transform;
bones[1].parent = transform;
bones[1].localRotation = Quaternion.Euler(0, 180, 0);
bones[1].localPosition = new Vector3(0, 5, 0);
and does not work, the bones are not rotating.
What am I doing wrong?
(spanish to english)
Answer by Paulius-Liekis · Apr 18, 2011 at 06:10 AM
Do you do this as well (taken from your link):
bindPoses[1] = bones[1].worldToLocalMatrix * transform.localToWorldMatrix;
This kind of "compensates" for the rotation that you just set. You need to change position/rotation of bone after that (better in runtime to see the results).
Do you really need to create meshes from code?
Answer by montblack · Apr 18, 2011 at 02:57 PM
yes i do that the complete code is:
using UnityEngine; using System.Collections; using System; public class example : MonoBehaviour { Transform[] bones; void Start() { gameObject.AddComponent<Animation>(); gameObject.AddComponent<SkinnedMeshRenderer>(); SkinnedMeshRenderer renderer = GetComponent<SkinnedMeshRenderer>(); Mesh mesh = new Mesh(); mesh.vertices = new Vector3[] {new Vector3(-1, 0, 0), new Vector3(1, 0, 0), new Vector3(-1, 5, 0), new Vector3(1, 5, 0)}; mesh.uv = new Vector2[] {new Vector2(0, 0), new Vector2(1, 0), new Vector2(0, 1), new Vector2(1, 1)}; mesh.triangles = new int[] {0, 1, 2, 1, 3, 2}; mesh.RecalculateNormals(); renderer.material = new Material(Shader.Find(" Diffuse")); BoneWeight[] weights = new BoneWeight[4]; weights[0].boneIndex0 = 0; weights[0].weight0 = 1; weights[1].boneIndex0 = 0; weights[1].weight0 = 1; weights[2].boneIndex0 = 1; weights[2].weight0 = 1; weights[3].boneIndex0 = 1; weights[3].weight0 = 1; mesh.boneWeights = weights; bones = new Transform[2]; Matrix4x4[] bindPoses = new Matrix4x4[2]; bones[0] = new GameObject("Lower").transform; bones[0].parent = transform; bones[0].localRotation = Quaternion.identity; bones[0].localPosition = Vector3.zero; bindPoses[0] = bones[0].worldToLocalMatrix * transform.localToWorldMatrix; bones[1] = new GameObject("Upper").transform; bones[1].parent = transform; bones[1].localRotation = Quaternion.Euler(0, 180, 0); bones[1].localPosition = new Vector3(0, 5, 0); bindPoses[1] = bones[1].worldToLocalMatrix * transform.localToWorldMatrix; mesh.bindposes = bindPoses; renderer.bones = bones; renderer.sharedMesh = mesh; AnimationCurve curve = new AnimationCurve(); curve.keys = new Keyframe[] {new Keyframe(0, 0, 0, 0), new Keyframe(1, 3, 0, 0), new Keyframe(2, 0.0F, 0, 0)}; AnimationClip clip = new AnimationClip(); clip.SetCurve("Lower", typeof(Transform), "m_LocalPosition.z", curve); animation.AddClip(clip, "test"); animation.Play("test"); }
void Update(){
try{
for ( int i = 0; i < bones.Length; i++ ){
Debug.DrawLine (bones[i].transform.position,bones[i].parent.transform.position, Color.green);
}
}catch(Exception e){}
}
}
I see the inspector that applies a rotation, but graphically the object is in the same position and without rotating, try using that code and rotate in different angles, and you will see that they will always get the same result. And yes, i need to create the mesh with code, becouse i'm allready reading a xml format, and greatly reducing the number of vertices when importing.
i need a rotation like this:
please help me i really need this.
Greetings from Colombia!
Your answer
Follow this Question
Related Questions
I need a point midway between two bones' rest positions 1 Answer
How to make a bone rotate facing towards a given Transform? 0 Answers
Bone doesn't rotate through script 1 Answer
How can I rotate a bone from script? Applications: Move crane arm, point gun... 2 Answers
Why does rotating a bone on one axis affect others and translation 2 Answers