- Home /
Moving bones of a model through code
Hi, Lets see if I can explain myself clearly: I have a rig arm with four bones, a root bone which is at the base, a tip bone at the top and two middle bones. What I want to do is to move the middle bones as if they were following the tip bone so if I move the tip bone diagonally to my up and right position of the screen my middle bones will follow in a normal looking way so that my arm looks as is extending diagonally, a and the game is a 2d sidescroller so I don't have to worry about z position. Right now I have the movement in the y axis working somewhat as I want with this code:
//stores the bone that is going to be pulled by this one
public GameObject previousBone;
//when true this allows bone movement, this is for testing purposes
public bool boneMoving = false;
//stores the bonecontroller script from the other bone
BoneController control;
//diference in distances when the previous bone will start moving
public float yDif = 1f;
//percentage of the distance the previous bone will move
public float yDiv = .8f;
void Start ()
{
control=previousBone.transform.GetComponent<BoneController>();
}
void Update ()
{
if(boneMoving)
{
if(this.transform.position.y > previousBone.transform.position.y + yDif)
{
previousBone.transform.position = new Vector3(this.transform.position.x,this.transform.position.y * yDiv, this.transform.position.z);
}
else if(this.transform.position.y < previousBone.transform.position.y + yDif)
{
previousBone.transform.position = new Vector3(this.transform.position.x, this.transform.position.y * yDiv, this.transform.position.z);
}
if(control != null)
{
control.boneMoving = true;
}
}
else
{
if(control != null)
{
control.boneMoving = false;
}
}
}
This is only for vertical movement but I can't seem to figure out diagonal movement any suggestions are more than welcome thanks.
Answer by lauralexis · Nov 18, 2016 at 07:44 AM
I think that what you are looking for is inverse kinematics (IK). Unfortunately, I can not help you much with that, but you can search for some tutorials on IK on YouTube or here in the forums.
Your answer
![](https://koobas.hobune.stream/wayback/20220613093441im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I make a certain bone rotate with the camera? 2 Answers
Remove bone from animator/rig 0 Answers
Can't move mesh limbs? 1 Answer
How to handle high heel footwear in Unity characters/rigs? 2 Answers