- Home /
Changing child's rotation changes parent's position
Tried to search the answer for it but found nothing so i think i am missing something very obvious here.
I have a main camera as a child of a vehicle. In the script attached to the camera i am reseting camera's rotation so it is not rotating with the vehicle, just follow it:
transform.rotation = Quaternion.Euler(0,-180,-180);
However the moment the vehicle changes it's rotation, all hell breaks loose: this single line of code somehow magically causes the vehicle to fly out of the screen (to be exact: it changes it's position by hundreds of units per second.)
Can someone explain to me what is happening exactly ? Very thanks.
EDIT: The vehicle is a space ship (no wheels, no physics) actually and it moves forward with:
transform.Translate(Vector3.forward * speed * Time.deltaTime)
and to the sides:
mousePosition = Input.mousePosition;
mousePosition.z = 50; //distance to camera
touchPosition = Camera.main.ScreenToWorldPoint(mousePosition);
direction.x = touchPosition.x;
direction.z = transform.position.z; //player controls movement
//on x-axis only
transform.position = Vector3.Lerp(transform.position, direction, 0.1f);
Without seeing the code to move the vehicle, it is difficult to offer any solution. As a guess, some aspect of your vehicle movement is dependent on the rotation of the camera. Changing rotation either results in new direction of movement or in a collision which caused new movement.
Are physics constraints involved? Like hinges of some sort?
Could be a feedback effect, wheel changes, so the hinge adjust the parent if they're connected somehow, which changes the child wheel again, which changes the parent - with unstable velocity being added in every step.