void AttachHead(Collider new_head) { SpringJoint spring = GetComponent <SpringJoint>(); RemoveLine(); if (spring != null) { Destroy(spring); } Vector3[] faces = { new_head.transform.up, -new_head.transform.up, new_head.transform.right, -new_head.transform.right, new_head.transform.forward, -new_head.transform.forward }; float greatest_dot = 0.0f; Vector3 attach_face = Vector3.zero; for (int i = 0; i < faces.Length; ++i) { float dot = Vector3.Dot(-transform.up, faces[i]); if (dot > greatest_dot) { attach_face = faces[i]; greatest_dot = dot; } } new_head.transform.rotation = Quaternion.FromToRotation(attach_face, -transform.up) * new_head.transform.rotation; greatest_dot = 0.0f; Vector3 robot_face_vector = Vector3.zero; for (int i = 0; i < faces.Length; ++i) { float dot = Vector3.Dot(transform.forward, faces[i]); if (dot > greatest_dot) { robot_face_vector = faces[i]; greatest_dot = dot; } } FixedJoint head_joint = gameObject.AddComponent <FixedJoint>(); head_joint.connectedBody = new_head.GetComponent <Rigidbody>(); head_joint.anchor = headCenterOffset; head_joint.autoConfigureConnectedAnchor = false; head_joint.connectedAnchor = Vector3.zero; head_joint.breakForce = breakForce; head_joint.breakTorque = breakTorque; if (robot_head_ != new_head.GetComponent <RobotHead>()) { robot_head_ = new_head.GetComponent <RobotHead>(); robot_head_.SetBody(this); robot_head_.SetOrientation(transform.up, robot_face_vector); robot_head_.BootUp(); } }
void DisconnectHead() { if (robot_head_ != null) { robot_head_.ShutDown(); robot_head_.SetBody(null); robot_head_ = null; } }
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; }
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; } } }
public Robot WithHead(RobotHead head) { return(With(head.Prop)); }
void AttachHead(Collider new_head) { SpringJoint spring = GetComponent<SpringJoint>(); RemoveLine(); if (spring != null) Destroy(spring); Vector3[] faces = {new_head.transform.up, -new_head.transform.up, new_head.transform.right, -new_head.transform.right, new_head.transform.forward, -new_head.transform.forward}; float greatest_dot = 0.0f; Vector3 attach_face = Vector3.zero; for (int i = 0; i < faces.Length; ++i) { float dot = Vector3.Dot(-transform.up, faces[i]); if (dot > greatest_dot) { attach_face = faces[i]; greatest_dot = dot; } } new_head.transform.rotation = Quaternion.FromToRotation(attach_face, -transform.up) * new_head.transform.rotation; greatest_dot = 0.0f; Vector3 robot_face_vector = Vector3.zero; for (int i = 0; i < faces.Length; ++i) { float dot = Vector3.Dot(transform.forward, faces[i]); if (dot > greatest_dot) { robot_face_vector = faces[i]; greatest_dot = dot; } } FixedJoint head_joint = gameObject.AddComponent<FixedJoint>(); head_joint.connectedBody = new_head.GetComponent<Rigidbody>(); head_joint.anchor = headCenterOffset; head_joint.autoConfigureConnectedAnchor = false; head_joint.connectedAnchor = Vector3.zero; head_joint.breakForce = breakForce; head_joint.breakTorque = breakTorque; if (robot_head_ != new_head.GetComponent<RobotHead>()) { robot_head_ = new_head.GetComponent<RobotHead>(); robot_head_.SetBody(this); robot_head_.SetOrientation(transform.up, robot_face_vector); robot_head_.BootUp(); } }
public void SetRobotHead(RobotHead head) { Head = head; }