private void OnTriggerEnter(Collider other) { if (triggerSwitch && !controller.pausetriggers && IsAttachmentPointEmpty() && other.tag == "AttachableObject") { GameObject obj = other.gameObject; attachableObject = obj.GetComponent <AttachableObject>(); if (attachableObject != null && attachmentPointID == attachableObject.attachPoint) { triggerSwitch = false; controller.AttachObject(obj, transform); } } }
private void OnCollisionStay2D(Collision2D collision) { if (attachedObject == null && collision.gameObject.GetComponent <AttachableObject>() != null && attachCooldown < 0.001f) { AttachableObject collidedObject = collision.gameObject.GetComponent <AttachableObject>(); attachedObject = collidedObject; collidedObject.AddAttacher(gameObject); transform.SetParent(collidedObject.transform); ContactPoint2D contact = collision.contacts[0]; Vector2 direction = new Vector2(contact.point.x - collision.gameObject.transform.position.x, contact.point.y - collision.gameObject.transform.position.y); float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward); Rigidbody2D r2D = gameObject.GetComponent <Rigidbody2D>(); r2D.simulated = false; } }
//attaching another object to this private void OnCollisionEnter(Collision other) { if (!m_canAttach) { return; } //ensure the other object can actually be attached to AttachableObject _att = other.transform.GetComponent <AttachableObject>(); if (_att != null && !m_connectedObjects.ContainsKey(_att)) { //point of contact between the attachment and the object ContactPoint _contactpoint = other.GetContact(0); //ignore collisions in future between these objects as they're technically "the same" Physics.IgnoreCollision(m_attachmentCollider, other.collider, true); CreateAttachment(_att, other.collider, _contactpoint); } }
/// <summary> /// have to do this after the frame ends as joints don't reliably break on the same frame as OnJointBreak is called /// </summary> IEnumerator LateUpdateJoints() { yield return(new WaitForEndOfFrame()); //find all objects' collider without the associated joint (i.e. the joint(s) that broke) foreach (Collider disc in m_connectedObjects.Where(o => (o.Value == null)).Select(o => o.Key.GetComponent <Collider>()).ToList()) { if (disc == null) { continue; } AttachableObject a = disc.GetComponentInParent <AttachableObject>(); if (a) { a.m_attachments.Remove(this); } Physics.IgnoreCollision(m_attachmentCollider, disc, false); m_connectedObjects.Remove(a); } }
public void FindFullObject(AttachableObject start, List <GameObject> result) { result.Add(start.gameObject); foreach (Attachment a in start.m_attachments) { if (result.Contains(a.gameObject)) { continue; } result.Add(a.gameObject); foreach (AttachableObject ao in a.m_connectedObjects.Keys) { if (result.Contains(ao.gameObject)) { continue; } FindFullObject(ao, result); } } }
void CreateAttachment(AttachableObject otherAtt, Collider col, ContactPoint contactPointWorld) { //create the configurable joint between the attachment and the object ConfigurableJoint _j = gameObject.AddComponent <ConfigurableJoint>(); //inset the attachment if necessary Vector3 _attPoint = (contactPointWorld.point); if (m_connectionType != ConnectionType.BallJoint) { float inset = Vector3.Distance(contactPointWorld.point, transform.TransformPoint(GetComponentInChildren <Rigidbody>().centerOfMass)) / 2f; Vector3 offset = (contactPointWorld.normal.normalized * inset / 8f); //don't autosnap when there's already something if (m_connectedObjects.Count != 0) { offset = Vector3.zero; } _attPoint = (contactPointWorld.point - offset); transform.position = _attPoint; } _j.anchor = transform.InverseTransformPoint(_attPoint); _j.connectedBody = otherAtt.GetComponent <Rigidbody>(); //anchor joint to other's position _j.autoConfigureConnectedAnchor = false; //set joint position to attachment point position _j.connectedAnchor = otherAtt.transform.InverseTransformPoint(_attPoint); //lock off motion _j.xMotion = ConfigurableJointMotion.Locked; _j.yMotion = ConfigurableJointMotion.Locked; _j.zMotion = ConfigurableJointMotion.Locked; if (m_connectionType == ConnectionType.BallJoint && m_connectedObjects.Count == 0) { //transform.position = contactPointWorld.point; //_j.anchor = Vector3.zero; _j.angularXMotion = _j.angularYMotion = _j.angularZMotion = ConfigurableJointMotion.Locked; } if (m_connectionType == ConnectionType.Glue) { _j.angularXMotion = _j.angularYMotion = _j.angularZMotion = ConfigurableJointMotion.Locked; JointDrive _d = new JointDrive { positionSpring = 1000f, positionDamper = 1000f }; _j.angularXDrive = _j.angularYZDrive = _d; } //_j.configuredInWorldSpace = true; _j.enableCollision = true; //break force _j.breakForce = 750f; m_connectedObjects.Add(otherAtt, _j); otherAtt.m_attachments.Add(this); }