Hey guys! I am looking to have my player bound to a string essentially, with some elasticity and am not quite sure how to go about achieving this. I was thinking some kind of hinge joint but I am not 100% sure. Basically the player can move anywhere they want stretching out the string, but when they are not moving they will snap back to the strings origin. So when they move, the string will stretch out with them. Thanks guys!
You'll want to move your character with AddForce(vector2, ForceMode2D.Impulse). and connect it using a SpringJoint2D most likely. https://docs.unity3d.com/Manual/class-SpringJoint2D.html
The character is already moving with AddForce, but I have never heard of a spring joint.. hm.. Thanks so much! Checking out the docs now! EDIT: Okay so this looks like it may be what I am looking to do! The next step is making this something visual on the screen? How would I have say, a rubbery rope for example, like a vector that stretches with the joint?
If you have a rope sprite, in the sprite editor you can set the pivot to be the very end and center of it. Then you can set its position to one end of the spring joint, and each frame update its scale and rotation to stretch it to the other point. I wrote a script to do that previously, so see if this works for you: Code (CSharp): using UnityEngine; public class StretchToEndPoint : MonoBehaviour { [Tooltip("The point to stretch to.")] public Transform endPoint; [Tooltip("If true, will rotate and stretch the X Axis, otherwise Y Axis.")] public bool useXAxis; [Tooltip("If true, will use the negative axis instead of positive.")] public bool negativeAxis; private SpriteRenderer mySpriteRenderer; private float currentDistance; private Vector2 spriteSize; private void Awake() { mySpriteRenderer = GetComponent<SpriteRenderer>(); spriteSize = mySpriteRenderer.sprite.bounds.size; } private void Update() { UpdateRotation(); UpdateScale(); } private void UpdateRotation() { // get the vector between this object and the endPoint Vector3 difference = endPoint.position - transform.position; // get the length of that vector currentDistance = difference.magnitude; // point the Z axis forwards, and the Y axis in the direction of the endpoint. Quaternion newRotation = Quaternion.LookRotation(Vector3.forward, difference.normalized); if(useXAxis) { // rotate an extra 90 degrees to point the X axis instead of Y newRotation *= Quaternion.Euler(0, 0, 90); } // apply the new rotation transform.rotation = newRotation; } private void UpdateScale() { // get the correct sprite dimension float spriteLength = useXAxis ? spriteSize.x : spriteSize.y; // divide the distance to stretch by the length of the sprite // ( this is what the scale needs to be to reach the endPoint ) float newAxisScale = currentDistance / spriteLength; // make sure the scale is negative if necessary if(negativeAxis && newAxisScale > 0) { newAxisScale *= -1; } Vector3 newScale = Vector3.one; // alter the proper axis' scale if(useXAxis) { newScale.x = newAxisScale; } else { newScale.y = newAxisScale; } // apply the scale transform.localScale = newScale; } }