- Home /
How to apply parent based rotation?
Hello everyone. So, i'm learning to code by re-making popular games on my own. Currently i'm working on an AngryBirds clone and i got a problem with blue bird mechanics.
FAQ: The bluebird (big bird) in angry birds is thrown like a regular bird, but when you tap on a screen again, it is devided into 3 smaller birds, all of which continue to move at a direction based on a direction a bigger bird had before separation.
That doesn't matter whether the big bird fly to the right of the screen or straight downwards or else, it always seem to separate in 3 birds, of which the middle one following the same direction, and with the corner birds going like ~ +15 and ~ -15 degree from the original direction.
Problem: Unfortunately i have no idea, how to "get" the rotation, from the direction it was moving, to add +15 and -15 degrees to corner birds. Currently, to spawn them i use:
Instantiate(smallBird, position, Quaternion.Euler(0, 0, 15));
Instantiate(smallBird, position, Quaternion.Euler(0, 0, 0));
Instantiate(smallBird, position, Quaternion.Euler(0, 0, -15));
That is totally wrong, as you understand, because it always spawn them facing the same angles. I believe there is a way to somehow put all the direction information together to initially spawn them rotated at right angles on Z axis. Then, using "Vector3.right" to apply force to move them "forward" (X axis).
Important: I'm using 3D physics with Z axis position and X,Y axis rotation freezed.
After the big bird is launched it doesn't rotate at all until it hit something, so i guess i can only get rotation using direction it was moving. (Somehow).
Or maybe if there is a way i can make big bird turn to the direction it is flying on it's X axis, it could solve the problem, too.
Here is a piece of launch code i use, for that case:
startPosition = birdBody.transform.position;
void OnMouseDrag()
{
if (active == true && thrown == false)
{
mousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 15));// 15 потому что камера по Z в -10.
Vector3 originClampPoint = mousePosition - startPosition;
transform.position = Vector3.SmoothDamp(startPosition + Vector3.ClampMagnitude(originClampPoint, dragRange), mousePosition, ref velocity, smoothDrag, maxSpeed);
}
}
void OnMouseUp()
{ //something not important//
birdBody.AddForce((startPosition - dragPosition) * throwPower, ForceMode.VelocityChange);
}
And again, after the launch it doesn't rotate at all.
Hopefully either of solutions is achievable, i would be glad to hear any working anyway. Thx in advance, and sorry for bad English.
Answer by KoenigX3 · Jan 05, 2017 at 07:45 PM
As i can remember, in the original AngryBirds the parent bird does not rotate, it stays at the identical rotation. However, i can give you a solution.
If you want to rotate the parent bird into the direction of movement, you can use this:
transform.LookAt(transform.LookAt(transform.position + birdBody.velocity);
From now, you can use the rotation of the parent bird to spawn the children:
Instantiate(smallBird, position, Quaternion.Euler(transform.rotation.EulerAngles + new Vector3(0, 0, 15)));
Instantiate(smallBird, position, transform.rotation);
Instantiate(smallBird, position, Quaternion.Euler(transform.rotation.EulerAngles - new Vector3(0, 0, 15)));
Maybe you need to switch the operators on the first and the third child (i don't know which one faces up or down).
(Note: If the script is not on the parent bird, you should change transform.position and transform.rotation to the parent bird's transform.position and transform.rotation in every case)
I hope that this will help you in solving the problem.
Worked for me, but i had to change
Instantiate(smallBird, position, Quaternion.Euler(transform.rotation.EulerAngles + new Vector3(0, 0, 15)));
for
Instantiate(smallBird, position, Quaternion.Euler(transform.rotation.EulerAngles + new Vector3(15, 0, 0)));
Adding angle to Z axis messed everything, because "LookAt" function makes it fly with Z axis "forward" not with X axis:
Also you doublewrite transform.LookAt. Well, at least for me that worked only in this form:
transform.LookAt(transform.position + birdBody.velocity);
Yeah, as i've said in original AngryBird they doesn't rotate while in flight, but this is nice solution as to me. Thank you!