- Home /
Move child closer to parent ?
So my character has 3 sprites in which one of them his shoulder moves slightly to the right of the screen, and my characters arm got an separate sprite, so when the shoulder goes to the right, his arm doesnt goes along (It gets clear in the screenshot). To resolve this, I tried this script:
void Start() {
parentPos = new Vector2(this.transform.localPosition.x,this.transform.localPosition.y);
}
void Update () {
if (this.transform.parent.GetComponent<SpriteRenderer>().sprite.name == "playersprite_cima") {
this.transform.position = parentPos;
}
}
So what Im trying to achieve with this script is to move the arm of my character to the same position of the parent which is the rest of the body, when the specific sprite is active. But i dont think im doing it the right way. Screenshots with the results:
I guess this should work:
void Start() {
parentPos = new Vector2(this.transform.localPosition.x,this.transform.localPosition.y);
}
void Update () {
if (this.transform.parent.GetComponent<SpriteRenderer>().sprite.name == "playersprite_cima") {
this.transform.localPosition = parentPos;
}
}
This won't work, since you set local position of arm to something that is local position of parent. If parent will be at position 10,10
, arm would be at 10, 10
RELATIVE to parent.
He wants it to be 0,0
relative to parent! So to achive that, either set .position
(which is global) of an arm with a .position
of a parent. Or set .localPosition
of an arm to Vector3.zero
.
After you will set that, please take a note that PIVOT of an arm will be set to the same position as a PIVOT of a parent. To let it be set to the exact position you want, either add an offset to the position, or make an empty gameObject to act as a transform to move the arm to, ins$$anonymous$$d of a parent (and child this gameObject to the parent).
Answer by hameed-ullah-jan · Jul 06, 2018 at 11:29 AM
you can take a reference point for the arm, make that reference point child of the main object so that it can move along with the parent object, then try assigning the position of the reference point to the arm.
Your answer
Follow this Question
Related Questions
2d Circle won't change size 0 Answers
Why transform class list give me error? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unity 2D Enemy Frog Jumping Issue It Keeps On Getting Lower & Lower??? 1 Answer