- Home /
Set the Break Point of a SpringJoint2D
Hi everyone!
I have a simple mockup game with some draggable ghosts (sprites). Sometimes the sprites will be stuck in the level obstacles, and i need the springJoint2D to "break" when the distance between the Sprite and the Cursor are too long. Like a "MaxDistance" value.
The SpringJoint is place via script, and I can't figure out how to listen neither the _SpringJoint2d.anchor or _SpringJoint.connectedAnchor positions.
like this.
I am using something like:
if ("Distance between" > MaxDistance) {
StopCoroutine ("DragObject");
};
But i dont know how to describe this "Distance between"...
Someone have a solution for this?
Answer by Milo_del_mal · Feb 17, 2016 at 06:07 PM
A vector minus a vector gives the length between those two vectors... The magnitude is the distance... But you want to use square magnitude.
/ PSEUDOCODE ***/ distanceSquared = (itemPosition - jointPosition).sqrMagnitude;
if(Mathf.Pow(distanceThreshold, 2) < distanceSquared){ //do logic. }
Usage of sqr$$anonymous$$agnitude is to save on processing.
http://docs.unity3d.com/ScriptReference/Vector3-sqr$$anonymous$$agnitude.html
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to disable script ,How to disable a script in multiple Gameobjects from another script? 1 Answer
Reduce Physics2D Lag 0 Answers
How to detect when a 2D joint is broken? 0 Answers