- Home /
How do I make the length of my rope shorter
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: https://www.youtube.com/watch?v=TB9RKZrgGXY&t=100s
Answer by Zymurer · Jul 15, 2018 at 09:19 AM
have you tried using the method "transform.localScale"
Hre is a an example code for 3d transform.localScale += new Vector3(0.1F, 0, 0);
To make it for 2d just change Vector3
to Vector2
and delete one of the axes. Your code should look something like that:
transform.localScale += new Vector2(0, 0);
Your answer
Follow this Question
Related Questions
2D 360 degress platformer example needed 0 Answers
Velocity Movement & Physics Interactions by Rigidbody2D 0 Answers
2d physics 1 Answer
Using HingeJoint2D to Join Objects at Specific Points 2 Answers
2D Slope acceleration 1 Answer