Simple parent/child transform - child not following rotation
I'm building a top down snake game (3D view).
'Snake head' is an empty transform with several mesh gameobjects under it to make up the visual for the snake's head.
'Snake head' has a script on it;
public class SnekController : MonoBehaviour {
public float translateSpeed;
public float rotateSpeed;
private void Update() {
// Translation...
this.transform.Translate(this.transform.forward * this.translateSpeed * Time.deltaTime);
// Rotation...
if (!Input.GetMouseButton(0))
return;
float halfWidth = Screen.width / 2;
this.Turn(Input.mousePosition.x < halfWidth ? -1 : 1);
}
private void Turn(int _direction) {
this.transform.Rotate(0, this.rotateSpeed * _direction * Time.deltaTime, 0);
}
}
The snake turns left or right depending on if the mouse was pressed on the left or right side of the screen. It seemed to work, when the head just had a non-descript sphere under it.
Now that I've added a tongue and some eyes (children of the 'snake head') - they themselves are NOT rotating along with the parent ... OR rather ... although I'm telling the 'snake head' to translate along it's forward axis, I've noticed that as I turn it seems to not do that; for example if I turn 90 degrees the head will now be moving along it's X axis.
I gotta be doing something really stupid, but I just cannot see it.
There is no other code anywhere else, just the controller I've posted.
Thanks for looking!
Answer by whoshotdk · Sep 23, 2020 at 05:16 PM
It's always the same, you spend 20 minutes writing out the problem and then 2 minutes later you discover the solution yourself.
Turns out 'transform.Translate' works in local-space by default. Adding 'Space.World' as the last parameter seems to have sorted everything out.
Your answer
![](https://koobas.hobune.stream/wayback/20220613012425im_/https://answers.unity.com/themes/thub/images/avi.jpg)