void Update()
    {
        if (GetBody() == null)
        {
            return;
        }

        int head_mask = LayerMask.GetMask("BoxHead");

        Collider[] heads            = Physics.OverlapSphere(transform.position, loveRange, head_mask);
        Transform  target           = null;
        float      closest_distance = loveRange;

        foreach (Collider head in heads)
        {
            RobotHead robot_head = head.GetComponent <RobotHead>();
            if (robot_head != null && robot_head.GetBody() != null && robot_head != this)
            {
                float distance = (robot_head.transform.position - transform.position).magnitude;
                if (closest_distance > distance)
                {
                    closest_distance = distance;
                    target           = robot_head.transform;
                }
            }
        }

        if (target == null)
        {
            NormalEyes();
            hearts.Stop();
        }
        else
        {
            if (!hearts.isPlaying)
            {
                hearts.Play();
            }
            CycleLoveyEyes();
            Vector3 direction = (target.position - transform.position).normalized;
            Vector3 torque    = loveRunSpeed * Vector3.Cross(direction, Vector3.up);
            GetBody().feet.GetComponent <Rigidbody>().AddTorque(torque);
        }

        if (GetBody().feet.IsUpright())
        {
            float progress   = (1.0f * beatSource.timeSamples) / beatSource.clip.samples;
            float spin_phase = spinsPerLoop * progress;
            spin_phase -= (int)spin_phase;

            float spin_torque = spinTorqueScale * spinTorqueCurve.Evaluate(spin_phase);
            GetComponent <Rigidbody>().AddTorque(spin_torque * GetBody().transform.up);
        }

        active_time_ += Time.deltaTime;
    }
Esempio n. 2
0
    void Update()
    {
        if (GetBody() != null)
        {
            SpringJoint bounce = robot_body_.feet.GetComponent <SpringJoint>();

            float progress    = (1.0f * beatSource.timeSamples) / beatSource.clip.samples;
            float dance_phase = dancesPerLoop * progress;
            dance_phase -= (int)dance_phase;

            if (dance_phase < danceDutyCycle && GetBody().feet.IsUpright())
            {
                bounce.connectedAnchor = (1 - danceScale) * resting_anchor_;
            }
            else
            {
                bounce.connectedAnchor = resting_anchor_;
            }

            if (GetBody().feet.IsUpright())
            {
                int        head_mask = LayerMask.GetMask("BoxHead");
                Collider[] heads     = Physics.OverlapSphere(transform.position, loverRadius, head_mask);
                Transform  chaser    = null;
                foreach (Collider head in heads)
                {
                    RobotHead lovey_head = head.GetComponent <RobotLoveyHead>();
                    if (lovey_head != null && lovey_head.GetBody() != null)
                    {
                        chaser = head.transform;
                    }
                }

                if (chaser != null)
                {
                    RunAway(chaser);
                    if (!running_)
                    {
                        SetEyes(leftScaredEye, rightScaredEye);
                    }
                }
                else if (running_)
                {
                    SetEyes(leftHappyEye, rightHappyEye);
                }
                running_ = chaser != null;
            }
        }
    }
    void LookForHead()
    {
        Vector3 neck_position        = transform.TransformPoint(headAttachmentOffset);
        Vector3 head_center_position = transform.TransformPoint(headCenterOffset);
        int     head_mask            = LayerMask.GetMask("BoxHead");

        Collider[] heads = Physics.OverlapSphere(neck_position, headMagnetRadius, head_mask);

        Collider closest_head     = null;
        float    closest_distance = headMagnetRadius;

        foreach (Collider head in heads)
        {
            Vector3   distance   = head.transform.position - neck_position;
            RobotHead robot_head = head.GetComponent <RobotHead>();
            if (distance.magnitude < closest_distance &&
                transform.InverseTransformDirection(distance).y >= minimumHeadHeightDifference &&
                robot_head != null && (robot_head.GetBody() == null || robot_head.GetBody() == this))
            {
                closest_head     = head;
                closest_distance = distance.magnitude;
            }
        }

        SpringJoint spring = GetComponent <SpringJoint>();

        if (closest_head == null)
        {
            DisconnectHead();
            RemoveLine();
            if (spring != null)
            {
                Destroy(spring);
            }
            GetComponent <AudioSource>().Stop();
        }
        else
        {
            if (closest_head.GetComponent <RobotHead>() != robot_head_)
            {
                DisconnectHead();
            }

            if ((head_center_position - closest_head.transform.position).magnitude < attachRadius)
            {
                AttachHead(closest_head);
                GetComponent <AudioSource>().Stop();
            }
            else
            {
                DisconnectHead();
                DrawLine(neck_position, closest_head.transform.position);
                if (spring == null)
                {
                    spring = gameObject.AddComponent <SpringJoint>();
                    GetComponent <AudioSource>().Play();
                }

                spring.connectedBody = closest_head.GetComponent <Rigidbody>();
                spring.anchor        = headCenterOffset;
                float magnet_force = maxMagnetForce *
                                     Mathf.Clamp((1 - closest_distance / headMagnetRadius), 0, 1);
                spring.spring      = magnet_force * magnet_force;
                spring.maxDistance = 0.0f;
                spring.autoConfigureConnectedAnchor = false;
                spring.connectedAnchor = Vector3.zero;
            }
        }
    }