Esempio n. 1
0
 public static void FollowTarget(AttachObject target, AttachObject follower,
                                 AttachStrategyFollowPositionOptions positionOptions,
                                 AttachStrategyFollowRotationOptions rotationOptions)
 {
     if (rotationOptions.behavior != RotationBehavior.DoNothing)
     {
         Quaternion desiredRotation = FollowingUtility.GetDesiredRotationForObject(follower,
                                                                                   rotationOptions.behavior, rotationOptions.GetRotationParamSelector(),
                                                                                   rotationOptions.GetLookAtPositionParamSelector(), rotationOptions.GetUpParamSelector(),
                                                                                   rotationOptions.GetForwardParamSelector(), rotationOptions.GetOffsetParamSelector());
         Quaternion nextRotation;
         if (rotationOptions.speed == 0)
         {
             nextRotation = desiredRotation;
         }
         else
         {
             nextRotation = Quaternion.RotateTowards(follower.attachable.transform.rotation, desiredRotation,
                                                     Time.deltaTime * rotationOptions.speed);
         }
         ClingyUtils.MoveRotation(nextRotation, follower.attachable.transform, rotationOptions.rotateMethod,
                                  follower.rigidbody, follower.rigidbody2D);
     }
     // todo - move before rotate, rotate before move...it's situationally dependent
     if (positionOptions.behavior != PositionBehavior.DoNothing)
     {
         FollowingObjectState state = GetObjectState(follower);
         Vector3 desiredPos         = FollowingUtility.GetDesiredPositionForObject(follower, positionOptions.behavior,
                                                                                   positionOptions.GetFollowerAnchorParamSelector(),
                                                                                   positionOptions.GetTargetAnchorParamSelector());
         if (!positionOptions.followX)
         {
             desiredPos.x = follower.attachable.transform.position.x;
         }
         if (!positionOptions.followY)
         {
             desiredPos.y = follower.attachable.transform.position.y;
         }
         if (!positionOptions.followZ)
         {
             desiredPos.z = follower.attachable.transform.position.z;
         }
         Vector3 nextPos;
         if (positionOptions.smoothing == 0)
         {
             nextPos = desiredPos;
         }
         else
         {
             nextPos = Vector3.SmoothDamp(follower.attachable.transform.position, desiredPos,
                                          ref state.velocity, positionOptions.smoothing);
         }
         ClingyUtils.MovePosition(nextPos, follower.attachable.transform, positionOptions.moveMethod,
                                  follower.rigidbody, follower.rigidbody2D);
     }
     // // note this sets localScale to lossyScale, so only works if there's no parent scale on the attacher
     // if (followScale)
     //     context.attacher.transform.localScale = context.receiver.transform.lossyScale;
 }
Esempio n. 2
0
        static FollowingObjectState GetObjectState(AttachObject obj)
        {
            FollowingObjectState state;

            if (obj.state == null)
            {
                state     = new FollowingObjectState();
                obj.state = state;
            }
            state = (FollowingObjectState)obj.state;
            return(state);
        }