Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class RopeTest : MonoBehaviour
- {
- public LineRenderer line;
- public Transform StartPoint;
- public Transform EndPoint;
- public RaycastHit hitStart;
- public RaycastHit hitEnd;
- public float length;
- public int Points = 1; // Keeps track of the total amount of points
- public Transform PointPositions; // This transforms position is set to the second to last point on the line.
- public Transform SecondEndPoint; // This transform should have the same position as the EndPoint. Its used for unwrapping the rope.
- private void Start()
- {
- PointPositions.position = StartPoint.position;
- }
- public LayerMask layersToHit;
- private void FixedUpdate()
- {
- line.SetPosition(0, StartPoint.position);
- line.SetPosition(Points, EndPoint.position);
- length = Vector3.Distance(PointPositions.position, EndPoint.position);
- //This Raycast starts from the PointPositions transform and goes to the EndPoint transform
- EndPoint.LookAt(PointPositions);
- if(Physics.Raycast(PointPositions.position, PointPositions.forward, out hitStart, length, layersToHit))
- {
- PointPositions.position = hitStart.point;
- CreatePoint();
- }
- // This Raycast goes from the EndPoint to the PointPositions transform to better detect if one of the raycasts hits the point.
- PointPositions.LookAt(line.GetPosition(Points));
- if (Physics.Raycast(EndPoint.position, EndPoint.forward, out hitEnd, length, layersToHit))
- {
- PointPositions.position = hitEnd.point;
- CreatePoint();
- }
- //This creates a Raycast when there are 2 or more points on the line. The raycast aims at the third to last point on the line and if it doesn't hit anything it unwraps.
- if (Points >= 2)
- {
- SecondEndPoint.LookAt(line.GetPosition(Points - 2));
- if (Physics.Raycast(SecondEndPoint.position, SecondEndPoint.forward, out hitEnd, length, layersToHit))
- {
- Debug.Log("DontUnwrap");
- }
- else
- {
- RemovePoint();
- }
- }
- }
- public float Distance;
- public float MinValue = 0.05f;
- public void CreatePoint()
- {
- //This makes sure that there are no overlapping points in the line.
- Distance = Vector3.Distance(PointPositions.position, line.GetPosition(Points - 1));
- if(Distance > MinValue)
- {
- Points = Points + 1;
- line.positionCount = line.positionCount + 1;
- line.SetPosition(Points - 1, PointPositions.position);
- line.SetPosition(Points, EndPoint.position);
- }
- }
- public void RemovePoint()
- {
- Points = Points - 1;
- line.positionCount = line.positionCount - 1;
- line.SetPosition(Points, EndPoint.position);
- PointPositions.position = line.GetPosition(Points - 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement