- Home /
Children must not flip with parent.
Hello. I've got a 2d platformer, and I've made character controller by the tutorial from unity's "Live Training".
Now I've got some problems with it. The movement to the left there realised through flipping X scale of the character, thus making him go left.
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
But here's a problem. When character moves left, all of his child objects also change their x-scale to -2 (i've made them 2.0 obviuosly up from 1), which I don't want, I want to rotate only parent object, but not the child.
If you can, help me how to make specific child not to flip with parent. Or all of them. I've been googling for 4 hours now, please, help T_T
Can explain why you need to keep the child element in scale, maybe there is a cleaner solution.
Answer by Kiwasi · Jul 03, 2014 at 11:44 PM
Iterate through each child and swap them back, as follows
foreach (Transform child in transform){
Vector3 childScale = transform.localScale;
childScale.x *= -1;
transform.localScale = theScale;
}
Answer by devcor · Jul 04, 2014 at 12:11 PM
I've made it more elegant way, without iterating through all of the children:
private GameObject child;
child = GameObject.Find ("SpriteRotate");
Vector3 childScale = child.transform.localScale;
childScale.x *= -1;
child.transform.localScale = childScale;
Answer by podmaster · Jul 04, 2014 at 01:02 PM
Find() its not recommended to use. You can create a new script "keepScale" attached to every child you want to keep in scale and do something like this:
var originalScaleToKeep : float;
function Start(){
originalScaleToKeep : float = transform.localScale.x;
}
function Update(){
transform.localScale.x = originalScaleToKeep;
}
The downside to this is that it runs every update cycle, use with caution
Add if statement as ,
if(transform.localScale.x <= 0 )
transform.localScale.x = ScaleTo$$anonymous$$eep ;
You could even use $$anonymous$$athf.Abs(float transform.localScale) ;
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Change character's Y rotation based on velocity. 1 Answer
[3D] Top-Down Make Character move in the direction of the position he is currently looking at? 1 Answer
Distribute terrain in zones 3 Answers
Deactivate Parent AND Children (C#) 1 Answer