Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Add constraint following hook example
#1
I'm following the RopeGrapplingHook example in order to achieve a similar example but with already created ropes. So my idea is that when I press a certain button a constraint is created between the rope and the player:
Code:
    public ObiRopeBlueprint m_ActualBlueprint;
    public ObiCollider m_Player;
    public ObiRope m_Rope;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.K))
        {
            AddConstraint();
        }
    }

    void AddConstraint()
    {
        var pinConstraints = m_ActualBlueprint.GetConstraintsByType(Oni.ConstraintType.Pin) as ObiConstraints<ObiPinConstraintsBatch>;
        var batch = pinConstraints.batches[0];
        batch.AddConstraint(0, m_Player, transform.localPosition, Quaternion.identity);
    }

But when I press the button I get the following error:
NullReferenceException: Object reference not set to an instance of an object
Obi.ObiPinConstraintsBatch.AddConstraint (System.Int32 index, Obi.ObiColliderBase body, UnityEngine.Vector3 offset, UnityEngine.Quaternion restDarboux) (at Assets/10-Vendors/Obi/Obi/Scripts/Common/Blueprints/Constraints/Batches/ObiPinConstraintsBatch.cs:82)
LianasTest.AddConstraint () (at Assets/10-Vendors/Obi/Obi/Scripts/RopeAndRod/Utils/Temporal/LianasTest.cs:26)
LianasTest.Update () (at Assets/10-Vendors/Obi/Obi/Scripts/RopeAndRod/Utils/Temporal/LianasTest.cs:17)


I assume that I'm doing something wrong, I have checked the batch var and it's not null so I'm not really sure what is the problem with AddConstraint.
PS: I made the following modification:
Code:
//var pinConstraints = m_ActualBluePrint.GetConstraintsByType(Oni.ConstraintType.Pin) as ObiConstraints<ObiPinConstraintsBatch>
var pinConstraints = m_Rope.GetConstraintsByType(Oni.ConstraintType.Pin) as ObiConstraints<ObiPinConstraintsBatch>

I read that blueprint acts as a prefab so in runtime cant be modified. Now I'm not getting any error but the constraint isn't happening anyway.

Regards!
Reply
#2
Hi there,

Asumiría, por el nombre de tu script ("Temporal/LianasTest"), que eres hispanohablante. Yo soy español, si prefieres podemos hablar por correo en español Sonrisa. Te contesto a este post en inglés:

Blueprints behave like prefabs indeed: they're used to instantiate particle/constraint data into the scene (in the form of a ObiActor, such as a rope), but the original blueprint data is not tied to existing instances. If you modify blueprint data, existing actors won't be affected. Only actors that you create from them on will "inherit" the changes made on the blueprint. So your second attempt is the correct one.

There's 2 important things missing from your script:
- You're not activating the constraints.
- You're not telling the actor to rebuild constraint batches. (Obi 5.6 and up only)

The correct code (in Obi 5.6) would be:

Code:
var pinConstraints = rope.GetConstraintsByType(Oni.ConstraintType.Pin) as ObiConstraints<ObiPinConstraintsBatch>;

//remove all existing pin constraint batches
pinConstraints.Clear();

// create a new batch and add one constraint to it:
var batch = new ObiPinConstraintsBatch();
batch.AddConstraint(rope.solverIndices[0], m_Player, transform.localPosition, Quaternion.identity); //<--- notice that we're using the solverIndices array

// activate that one constraint:
batch.activeConstraintCount = 1;

// add the batch:
pinConstraints.AddBatch(batch);

// notify the actor that constraints need to be rebuilt in the solver:
rope.SetConstraintsDirty(Oni.ConstraintType.Pin);


Also, check that pin constraints are globally enabled in your solver.
Reply