I picked up some code from a video that explained how to incorporate rope physics into my 2D game. Everything works as intended except for the fact that the length of the rope is too large for the small testing space that I made to present my game in. Basically, I am trying to cut the length of the rope down so I could start swinging earlier, but I do not know how to do so. Any help for this truly appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rope : MonoBehaviour {
public GameObject ropeShooter;
private SpringJoint2D rope;
public int maxRopeFrameCount;
private int ropeFrameCount;
public LineRenderer lineRenderer;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
Fire();
}
}
void FixedUpdate()
{
if (rope != null)
{
lineRenderer.enabled = true;
lineRenderer.positionCount = 2;
lineRenderer.SetPosition(0, ropeShooter.transform.position);
lineRenderer.SetPosition(1, rope.connectedAnchor);
}
else
{
lineRenderer.enabled = false;
}
}
void Fire()
{
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 position = ropeShooter.transform.position;
Vector3 direction = mousePosition;
RaycastHit2D hit = Physics2D.Raycast(mousePosition,direction,Mathf.Infinity);
if (hit.collider != null)
{
SpringJoint2D newRope = ropeShooter.AddComponent<SpringJoint2D>();
newRope.enableCollision = true;
newRope.frequency = .2f;
newRope.connectedAnchor = hit.point;
newRope.enabled = false;
GameObject.DestroyImmediate(rope);
rope = newRope;
ropeFrameCount = 0;
}
}
}
Also, here is a link to that youtube video I mentioned: Unity 2D Basic Rope Swinging Physics - YouTube