Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  How to generate rope from one point to next points ?
#1
Triste 
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….)


Attached Files Thumbnail(s)
   
Reply
#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
#3
Dedo arriba 
Thank you
Reply
#4
josemendez Is there a way for the rope that your script generates to have a material. I have been trying to generate a rope between two transforms during runtime preferably with a prefab but I haven't had any luck.  Huh
Reply
#5
(23-09-2021, 10:35 PM)Churger Wrote: josemendez Is there a way for the rope that your script generates to have a material. I have been trying to generate a rope between two transforms during runtime preferably with a prefab but I haven't had any luck.  Huh

Hi,

Of course. The rope is just an object like any other, so you do this the same way you do for any object in Unity: access the renderer and set the material.
Code:
rope.GetComponent<MeshRenderer>().material = <your material>
Reply
#6
Thanks for the solution!, however all I can get to show is this thin line? Sry if the image does not show I am new to this.
Reply
#7

Never mind I found the solution, I forgot to put the script on the solver, thank you so much for the help.
Reply