public void Attach(RaycastHit hit, float distance) { if (hit.collider != null && hit.collider.tag == "GrappleTarget") { if (rope.enabled) return; else distance = (hit.collider.gameObject.GetComponent<GrappleGuide>().GrappleTo() - transform.position).magnitude; } else { distance = (hit.point - transform.position).magnitude; } // Update grapple data list GrappleData grappleData = new GrappleData(); grappleData.gameObject = new GameObject("Grapple Placeholder"); if (hit.collider != null && hit.collider.tag == "GrappleTarget") { Transform t = hit.collider.gameObject.GetComponent<GrappleGuide>().grappleTo; CamTarget.Grappled(t); grappleData.gameObject.transform.position = t.position; } else { if (hit.collider != null) CamTarget.Grappled(hit.collider.transform); else CamTarget.Grappled(transform); grappleData.gameObject.transform.position = hit.point; } grappleData.maxDistance = distance; if (hit.collider != null) grappleData.gameObject.transform.parent = (grappleFollowMoving ? hit.collider.gameObject.transform : null); // Offset position a little so the rope is visible // grappleData.gameObject.transform.position += hit.normal * GrappleExtrusion; if(grapples.Count == 0) { grappleData.gameObject.transform.rotation = Quaternion.LookRotation(hit.normal) * Quaternion.Euler(90, 0, 0); } else { grappleData.gameObject.transform.LookAt(grapples[grapples.Count - 1].gameObject.transform, -rigidbody.velocity); grappleData.offset = hit.normal * GrappleExtrusion; } // Add grapple data to collection grapples.Add(grappleData); // Update grapple SetGrapple(grappleData); }
void SetGrapple(GrappleData grappleData) { // Move grapple to this position because it's dumb and stupid and dumb grapple.transform.position = transform.position; // Update spring joint springJoint.maxDistance = grappleData.maxDistance; springJoint.connectedBody = gameObject.GetComponent <Rigidbody>(); // Update grapple position and parent it so it follows moving objects grapple.transform.position = grappleData.gameObject.transform.position; if (grappleFollowMoving) { grapple.transform.parent = grappleData.gameObject.transform.parent; } // Make rope visible rope.enabled = true; }
public void Attach(RaycastHit hit, float distance, bool initialGrapple) { if (hit.collider != null && hit.collider.tag == "GrappleTarget") { if (rope.enabled) { return; } else { distance = (hit.collider.gameObject.GetComponent <GrappleGuide>().GrappleTo() - transform.position).magnitude; } } else { distance = (hit.point - transform.position).magnitude; } // Update grapple data list GrappleData grappleData = new GrappleData(); grappleData.gameObject = new GameObject("Grapple Placeholder"); if (hit.collider != null && hit.collider.tag == "GrappleTarget") { Transform t = hit.collider.gameObject.GetComponent <GrappleGuide>().grappleTo; if (initialGrapple) { CamTarget.Grappled(t); } grappleData.gameObject.transform.position = t.position; } else { if (initialGrapple) { if (hit.collider != null) { CamTarget.Grappled(hit.collider.transform); } else { CamTarget.Grappled(transform); } } grappleData.gameObject.transform.position = hit.point; } grappleData.maxDistance = distance; if (hit.collider != null) { grappleData.gameObject.transform.parent = (grappleFollowMoving ? hit.collider.gameObject.transform : null); } // Offset position a little so the rope is visible // grappleData.gameObject.transform.position += hit.normal * GrappleExtrusion; if (grapples.Count == 0) { grappleData.gameObject.transform.rotation = Quaternion.LookRotation(hit.normal) * Quaternion.Euler(90, 0, 0); } else { grappleData.gameObject.transform.LookAt(grapples[grapples.Count - 1].gameObject.transform, -GetComponent <Rigidbody>().velocity); grappleData.offset = hit.normal * GrappleExtrusion; } // Add grapple data to collection grapples.Add(grappleData); // Update grapple SetGrapple(grappleData); }
void LateUpdate() { // If we're grappling if (rope.enabled) { // If the rope can segment if (segment) { RaycastHit hitInfo; // Check for line of sight with previous grapples if (grapples.Count > 1) { int lastIndex = grapples.Count - 1; int oldIndex = lastIndex - 1; // Get old grapple GrappleData old = grapples[oldIndex]; // Calculate ray to grapple, adjusting distance a little so that it doesn't hit the object grapple is grappled on Vector3 difference = old.gameObject.transform.position - transform.position; Vector3 direction = difference.normalized; float distance = difference.magnitude - 0.1f; // If nothing hit, we have a clear line of sight to the grapple Debug.DrawRay(transform.position, direction); Ray ray = new Ray(transform.position, direction); if (!Physics.Raycast(ray, out hitInfo, distance)) { // Remove last grapple and reattach to the old one SetGrapple(grapples[oldIndex]); Destroy(grapples[lastIndex].gameObject); grapples.RemoveAt(lastIndex); } } // Check if the rope has collided with something if (Physics.Linecast(transform.position, grapple.transform.position, out hitInfo)) { // Create new grapple if far enough away from the last one float grappleDist = (grapple.transform.position - hitInfo.point).magnitude; if (grappleDist > minRopeDistanceForNewRope) { //float grapdiff = (grapple.transform.position - hitInfo.point).magnitude; Attach(hitInfo, springJoint.maxDistance - grappleDist, false); } } // Reel in if the currently attached object is a puller if (grapple.transform.parent != null) { const float minDistance = 1.0f; GameObject grappled = grapple.transform.parent.gameObject; Pull pull = grappled.GetComponent <Pull>(); if (pull != null || grappled.CompareTag("Pull")) { float rate = DefaultPullRate; if (pull != null) { rate = pull.rate; } float difference = rate * Time.deltaTime; foreach (GrappleData grappleData in grapples) { if (grappleData.maxDistance > minDistance + difference) { grappleData.maxDistance -= difference; } else { grappleData.maxDistance = minDistance; } } if (springJoint.maxDistance > minDistance + difference) { springJoint.maxDistance -= difference; } else { springJoint.maxDistance = minDistance; } } } } } }
void SetGrapple(GrappleData grappleData) { // Move grapple to this position because it's dumb and stupid and dumb grapple.transform.position = transform.position; // Update spring joint springJoint.maxDistance = grappleData.maxDistance; springJoint.connectedBody = gameObject.rigidbody; // Update grapple position and parent it so it follows moving objects grapple.transform.position = grappleData.gameObject.transform.position; if(grappleFollowMoving) grapple.transform.parent = grappleData.gameObject.transform.parent; // Make rope visible rope.enabled = true; }