c# - Can't collapse the rope by script -
i use rope script using hingejoint2d
can't collapse cuz understood hingejoint2d
tries keep distance between objects... how fix it?
signle solution come it's disable hingejoint2d
, rigitbody2d
, after changing of positions enable again...
below rope script:
namespace entities { [requirecomponent(typeof(rigidbody2d))] [requirecomponent(typeof(linerenderer))] public class rope : monobehaviour { private enum mode { none, rope, line, collapsing } public bool m_automatic = true; public gameobject m_target; public gameobject m_source; [range(1, 100)] public int m_segments = 1; public float m_mass = 1f; private linerenderer m_renderer; private mode m_mode = mode.none; private float m_timer; private float m_maxdistance; private float m_speed; private int m_activeindex; private gameobject[] m_nodes; private void awake() { m_renderer = getcomponent<linerenderer>(); if (m_automatic) { buildline(1f, 232f); } } private void update() { } private void lateupdate() { if (m_mode == mode.line) { m_timer -= time.deltatime; if (m_timer <= 0) { buildrope(); collapse(1f); } float curdistance = vector3.distance(m_target.transform.position, m_source.transform.position); if (curdistance >= m_maxdistance) { buildrope(); collapse(1f); } } else if (m_mode == mode.collapsing) { vector2 pos = m_nodes[m_activeindex].transform.position; vector2 targetpos = m_nodes[m_activeindex + 1].transform.position; pos = lerp(targetpos, pos, time.deltatime * m_speed); float distance = vector2.distance(pos, targetpos); if (distance <= 0.001f) { pos = targetpos; ++m_activeindex; if (m_activeindex == m_nodes.length - 2) { m_mode = mode.none; } } (int = 0; <= m_activeindex; ++i) { float z = m_nodes[i].transform.position.z; m_nodes[i].transform.position = new vector3(pos.x, pos.y, z); } } (int = 0; < m_nodes.length; ++i) { m_renderer.setposition(i, m_nodes[i].transform.position); } } private vector2 lerp(vector2 a, vector2 b, float factor) { float x = a.x - (a.x - b.x) * factor; float y = a.y - (a.y - b.y) * factor; return new vector2(x, y); } public bool visible { { return m_mode != mode.none; } } public void buildline(float timer, float maxdistance) { if (m_mode == mode.none) { m_mode = mode.line; m_timer = timer; m_maxdistance = maxdistance; m_renderer.positioncount = 2; m_nodes = new gameobject[2]; m_nodes[0] = m_source; m_nodes[1] = m_target; m_renderer.setposition(0, m_source.transform.position); m_renderer.setposition(1, m_target.transform.position); } } public void collapse(float speed) { if (m_mode == mode.rope) { m_mode = mode.collapsing; var srccollider = m_nodes[0].getcomponent<circlecollider2d>(); srccollider.enabled = false; m_speed = speed; m_activeindex = 0; } } public void buildrope() { if (m_mode != mode.rope && m_mode != mode.collapsing) { m_mode = mode.rope; int nodecount = m_segments + 1; m_nodes = new gameobject[nodecount]; m_nodes[0] = m_source; m_nodes[nodecount - 1] = m_target; var segmentoffset = (m_target.transform.position - m_source.transform.position) / m_segments; (int = 1; < m_segments; ++i) { var position = segmentoffset * + m_source.transform.position; buildnode(i, position, m_renderer.widthmultiplier); } var joint = m_target.addcomponent<hingejoint2d>(); joint.connectedbody = m_nodes[m_nodes.length - 2].getcomponent<rigidbody2d>(); m_renderer.positioncount = m_segments + 1; } } public void destroyrope() { if (visible) { m_mode = mode.none; (int = 0; < m_nodes.length - 1; ++i) { destroy(m_nodes[i]); } m_nodes = null; } } private void buildnode(int index, vector3 position, float radius) { var node = new gameobject("node " + index); node.transform.position = position; node.transform.parent = transform; var rigidbody = node.addcomponent<rigidbody2d>(); rigidbody.mass = m_mass; var joint = node.addcomponent<hingejoint2d>(); makeelastic(joint); m_nodes[index] = node; joint.connectedbody = m_nodes[index - 1].getcomponent<rigidbody2d>(); } private void makeelastic(hingejoint2d joint) { var limits = joint.limits; limits.min = -45; limits.max = 45; joint.limits = limits; joint.uselimits = true; } } }
the current bug when call collapse()
rope collapses short time , after becomes increase only.
Comments
Post a Comment