- Home /
How do I have a Child tell a Parent to move without the Child itself moving
I have an empty object with a player script with a head and body as its children. The head has a camera and a simple MouseLook script attached. The camera tells the player object which direction it is facing on the Y axis and the player then faces that direction so that everything points in the same direction on the Y axis while the head is free to look up and down as it pleases. The problem is that the head still rotates relative to the player and thus it doesn't line up under most scenarios.
I need a way for the MouseLook on the head to tell the player to look left and right without moving the head left and right, or, at the very least, a way to negate head movement on the Y axis relative to the player object.
Here is what I have so far
In my MouseLook script
void Update()
{
lookingY = transform.localEulerAngles.y - transform.parent.localEulerAngles.y;
player.Look(lookingY);
if (axes == RotationAxes.MouseXAndY)
{
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
}
the bit that goes "player.Look(lookingY);" hooks into this bit of code in the player script
public void Look (float lookingY) { transform.Rotate(new Vector3(0, lookingY, 0)); }
Update I did it! I put lookingY's assignment into the if statement and fed it directly into the player script while giving the Head's X rotation it's own little command and that seems to have done it.