【Unity】Dynamic modification of rope length based on Obi (ObiRopeCursor)

1. Change the rope length at runtime: ObiRopeCursor

Obi provides a very versatile component for modifying the length of a rope at runtime: ObiRopeCursor . When added to an ObiRope, the component will place a cursor at the top of it, allowing you to "add" or "remove" ropes in any direction from that point. Think of it like a normal cursor in a text editing application: you can place it anywhere in the text and add/remove characters in its place.

You just need to call its ChangeLength(length)method at runtime. The cursor will automatically take care of adding/removing particles/constraints on the rope to get the new length.

ObiRopeCursor has several properties:

  1. Cursor In
  2. Our Source
  3. Direction

1.1 Cursor In

This is the normalized position of the cursor and new particles will be added or removed . 0 will place it at the beginning of the rope, 1 will place it at the end of the rope. Any value in between will place it in the particle closest to that value.

1.2 Source Mu (源μ)

This is the normalized position of copied particles when adding new particles . 0 will place it at the beginning of the rope, 1 will place it at the end of the rope. Any value in between will place it in the particle closest to that value.

1.3 Direction

A Boolean value that controls the cursor direction. Enabling this will point the cursor to the end of the rope. Disabling it will make it point to the starting point.

For example, if you wanted to insert new particles in the middle of the rope by copying the first particle on the rope and make it grow towards the end (you probably fixed some particles at the beginning and wanted them to stay that way), you would set Cursor Mu is 0.5f , its Source Mu is 0 , and its direction is true .

Increasing the length of the rope using an ObiRopeCursor will use particles from the Blueprint's particle pool. Make sure the pool is large enough (by setting the Blueprint 's "pooled particles" property) to reach the desired maximum length. When reducing the length of the rope, unused particles are returned to the pool.

When working with the cursor, it is very useful to know the current length of the string. Use rope's RestLength property for this. The following code uses the W and S keys to increase or decrease the length of the rope. You can also control the speed of changes:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Obi;

public class RopeLengthController : MonoBehaviour {
    
    

	public float speed = 1;
	ObiRopeCursor cursor;
	ObiRope rope;

	void Start () {
    
    
		cursor = GetComponentInChildren<ObiRopeCursor>();
		rope = cursor.GetComponent<ObiRope>();
	}

	void Update () {
    
    
		if (Input.GetKey(KeyCode.W))
			cursor.ChangeLength(rope.restLength - speed * Time.deltaTime);

		if (Input.GetKey(KeyCode.S))
			cursor.ChangeLength(rope.restLength + speed * Time.deltaTime);
	}
}

Please add image description
Cursor (cursor) retract

Please add image description
Vernier (cursor) extension cord

Guess you like

Origin blog.csdn.net/weixin_44417441/article/details/135397233