Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Rope : MonoBehaviour
- {
- public int numJoints = 7;
- public Transform source;
- public Transform dest;
- Transform[] bones;
- // Use this for initialization
- void Start ()
- {
- bones = new Transform[numJoints];
- bones[0] = source;
- bones[numJoints - 1] = dest;
- for (int n = 1; n < numJoints - 1; ++n) {
- bones[n] = GameObject.CreatePrimitive (PrimitiveType.Sphere).transform;
- }
- }
- // Update is called once per frame
- void Update ()
- {
- float length = (dest.position - source.position).magnitude;
- for (int n = 1; n < numJoints - 1; ++n) {
- float i = Mathf.InverseLerp (0, bones.Length - 1, n);
- float si = Mathf.SmoothStep (0, 1, i);
- Vector3 sourceLine = source.position + source.forward * length * i;
- Vector3 destLine = dest.position - dest.forward * length * (1 - i);
- Vector3 pos = Vector3.Lerp (sourceLine, destLine, si);
- Quaternion rot = Quaternion.Slerp (source.rotation, dest.rotation, si);
- bones[n].position = pos;
- bones[n].rotation = rot;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment