if the bird is rotating clockwise then the right side of the bird should face the center, otherwise the left for counter-clockwise. keep that in mind for the next part.
to make one facing of an object align with the facing of another.
this is code i wrote to take the 2 nearest facings of 2 objects and rotate them to face (be parallel to eachother
/* Function:
* To take 2 objects and rotate object Rotate such that its face closest to SnapTo
* Params:
* Rotate: The object that you want to rotate.
* SnapTo: The object you want Rotate to snap to.
*
*/
public class AutoSnap : MonoBehaviour
{
Vector3 axis;
float angle;
Vector3 direction;
Ray ray;
RaycastHit SnapToHit;
RaycastHit RotateHit;
Vector3 MoveDistance;
AutoSnappedMove MoveComponent;
void Start(){
MoveComponent = GetComponent<AutoSnappedMove>();
ray = new Ray();
}
public void SnapToNearest (GameObject Rotate, GameObject SnapTo)
{
ray.origin = Rotate.transform.position;
ray.direction = SnapTo.transform.position - Rotate.transform.position;
Physics.Raycast (ray, out SnapToHit);
ray.origin = SnapTo.transform.position;
ray.direction = Rotate.transform.position - SnapTo.transform.position;
Physics.Raycast (ray, out RotateHit);
axis = Vector3.Cross (RotateHit.normal, -SnapToHit.normal);
if (axis != Vector3.zero) {
angle = Mathf.Atan2 (Vector3.Magnitude (axis), Vector3.Dot (RotateHit.normal, -SnapToHit.normal));
Debug.Log(Mathf.Rad2Deg);
Debug.Log(axis);
if(angle > .5f)
{
Rotate.transform.RotateAround (axis, angle);
}
}
MoveComponent.Move (RotateHit, SnapToHit);
}
in this case rotate would be the bird and snapto would be the tree
(this code is designed to make 2 objects snap together but it will work just fine for what you want which is to snap together at a distance)
you’ll want to replace
MoveComponent.Move (RotateHit, SnapToHit); (the last line of code)
with your own movement code
(presumably the transform.rotatearound())
these lines
ray.origin = SnapTo.transform.position;
ray.direction = Rotate.transform.position - SnapTo.transform.position;
Physics.Raycast (ray, out RotateHit);
are what work for myself. they find the NEAREST FACING of the moving(in your case bird)
object. You may want to change it to just the left or right side of the bird using a boolean (clockwise or counter clock) to determine which side
in that case you would remove that code and just do
axis = vector3.cross(transform.right/-transform.right, snaptohit.normal)
the tranform.right or -transform.right of course depending on left or right side of bird.
when you move you’ll want to set the distance from the trunk at some point thats easy
just pick a distance say 3 and begin the rotate when it’s approx 3 away as it moves in.
alternatly you can do
ray.direction = (bird.position - trunk.position)
ray.origion = trunk.position
ray.getpoint(distance)
or for a distance of 3
bird.transform.position = ray.getpoint(3);
also you could instead of moving towards the trunk you could slerp towards this point that is 3 units along the line drawn between the far trunk your aiming for and the bird however you would need to do a check to make sure the ray didnt pass through the tree it’s already currently circling (and if it did just call a wait command and wait half a second for the bird to circle more so it’s not in the way and try again)
otherwise it’d fly through the tree its already around and look wierd