Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  How to generate rope from one point to next points ?
#2
(15-09-2021, 10:29 PM)webmagic Wrote: Hi , let’s say we have 3 point in the scene (attached), how to generate rope from point 1 to point 2, and continue from point 2 to point 3? In runtime.

For example, there are some balls in the scene, when I press the button, a rope generated through these three points by order(like: 1~2~3~4~5….)

Hi there,

See:
http://obi.virtualmethodstudio.com/manua...ctors.html

Basically you create a new blueprint, and add one control point for each point you want the rope to pass trough. Here's a script that fully sets up a rope between two points and attaches it to them completely from scratch. Adding more points is trivial (just add more calls to AddControlPoint). Note the rope position and rotation is calculated from two points, so you might want to adjust it depending on your needs.

Code:
using UnityEngine;
using Obi;

public class RopeBetweenTwoPoints : MonoBehaviour
{
    public Transform start;
    public Transform end;
    public ObiSolver solver;

    void Start()
    {
        // Generate rope synchronously:
        Generate();
    }

    void Generate()
    {
        if (start != null && end != null)
        {
            // Adjust our transform:
            transform.position = (start.position + end.position) / 2;
            transform.rotation = Quaternion.FromToRotation(Vector3.right, end.position - start.position);

            // Calculate control point positions and tangent vector:
            Vector3 startPositionLS = transform.InverseTransformPoint(start.position);
            Vector3 endPositionLS = transform.InverseTransformPoint(end.position);
            Vector3 tangentLS = (endPositionLS - startPositionLS).normalized;

            // Create the blueprint:
            var blueprint = ScriptableObject.CreateInstance<ObiRopeBlueprint>();

            // Build the rope path:
            int filter = ObiUtils.MakeFilter(ObiUtils.CollideWithEverything, 0);
            blueprint.path.AddControlPoint(startPositionLS, -tangentLS, tangentLS, Vector3.up, 0.1f, 0.1f, 1, filter, Color.white, "start");
            blueprint.path.AddControlPoint(endPositionLS, -tangentLS, tangentLS, Vector3.up, 0.1f, 0.1f, 1, filter, Color.white, "end");
            blueprint.path.FlushEvents();

            // Generate particles/constraints:
            blueprint.GenerateImmediate();

            // Add rope actor / renderer / attachment components:
            var rope = gameObject.AddComponent<ObiRope>();
            var ropeRenderer = gameObject.AddComponent<ObiRopeExtrudedRenderer>();
            var attachment1 = gameObject.AddComponent<ObiParticleAttachment>();
            var attachment2 = gameObject.AddComponent<ObiParticleAttachment>();

            // Load the default rope section for rendering:
            ropeRenderer.section = Resources.Load<ObiRopeSection>("DefaultRopeSection");

            // Set the blueprint:
            rope.ropeBlueprint = blueprint;

            // Attach both ends:
            attachment1.target = start;
            attachment2.target = end;
            attachment1.particleGroup = blueprint.groups[0];
            attachment2.particleGroup = blueprint.groups[1];

            // Parent the actor under a solver to start the simulation:
            transform.SetParent(solver.transform);
        }
    }
}
Reply


Messages In This Thread
RE: How to generate rope from one point to next points ? - by josemendez - 16-09-2021, 09:04 AM