Quantcast
Channel: Questions in topic: "rope"
Viewing all articles
Browse latest Browse all 194

Need help with my rope mechanic

$
0
0
Hello, i am a noob in unity. I am trying to make a rope game that has two types of "ropes" - one normal and the second one which holds you at one angle and you can lengthen and shorten it (like Static hook from Terraria). I copied my code from first rope and added ability to change its length but i dont know how to do that rope holds you and dont let you swing. Here's my code: public LineRenderer line2; public DistanceJoint2D joint2; Vector3 targetPos; RaycastHit2D hit; public float distance = 10f; public LayerMask mask; void Start() { joint2 = GetComponent(); joint2.enabled = false; line2.enabled = false; } void FixedUpdate() { if (Input.GetKey(KeyCode.W)) { joint2.distance -= 0.1f; } if (Input.GetKey(KeyCode.S) && (joint2.distance < 8f)) { joint2.distance += 0.1f; } } void Update() { if (Input.GetKeyDown(KeyCode.Q)) { targetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); targetPos.z = 0; hit = Physics2D.Raycast(transform.position, targetPos - transform.position, distance, mask); if (hit.collider != null && hit.collider.gameObject.GetComponent() != null) { joint2.enabled = true; Vector2 connectPoint = hit.point - new Vector2(hit.collider.transform.position.x, hit.collider.transform.position.y); connectPoint.x = connectPoint.x / hit.collider.transform.localScale.x; connectPoint.y = connectPoint.y / hit.collider.transform.localScale.y; Debug.Log(connectPoint); joint2.connectedAnchor = connectPoint; joint2.connectedBody = hit.collider.gameObject.GetComponent(); joint2.distance = Vector2.Distance(transform.position, hit.point); line2.enabled = true; line2.SetPosition(0, transform.position); line2.SetPosition(1, hit.point); line2.GetComponent().grabPos = hit.point; line2.SetPosition(1, joint2.connectedBody.transform.TransformPoint(joint2.connectedAnchor)); } } if (Input.GetKey(KeyCode.Q)) { line2.SetPosition(0, transform.position); } if (Input.GetKeyUp(KeyCode.Q)) { joint2.enabled = false; line2.enabled = false; } } Please help i am stuck with it for like 5-6 hours also sorry for my english i dont know it well

Viewing all articles
Browse latest Browse all 194

Trending Articles