Advertisement
Dieton

WireGenV2

Sep 1st, 2023
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.90 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Unity.VisualScripting;
  4. using UnityEngine;
  5.  
  6. public class WireGenV2 : MonoBehaviour
  7. {
  8.     //[SerializeField] private Transform startTransform, endTransform;
  9.     //[SerializeField] private int segmentCount = 10;
  10.     [SerializeField] private float totalLength = 10;
  11.  
  12.     [SerializeField] private float radius = 0.5f;
  13.  
  14.     [SerializeField] private float totalWeght = 10;
  15.     [SerializeField] private float drag = 1;
  16.     [SerializeField] private float angularDrag = 1;
  17.  
  18.     [SerializeField] bool usePhysics = true;
  19.  
  20.     [SerializeField] private Transform[] segments;
  21.     //[SerializeField] private Transform segmentParent;
  22.  
  23.     private int prevSegmentCount;
  24.     private float prevTotalLenth;
  25.     private float prevDrag;
  26.     private float prevTotalWeight;
  27.     private float prevAngularDrag;
  28.     private float prevRadius;
  29.  
  30.     //Transform prevTreansform;
  31.  
  32.     // Start is called before the first frame update
  33.     private void Start()
  34.     {
  35.  
  36.     }
  37.  
  38.     // Update is called once per frame
  39.     private void Update()
  40.     {
  41.        
  42.         if (prevSegmentCount != segments.Length)
  43.         {
  44.             //RemoveSegments();
  45.             //segments = new Transform[segments.Length];
  46.             GenerateSegments();
  47.         }
  48.         prevSegmentCount = segments.Length;
  49.        
  50.  
  51.         if (
  52.             totalLength != prevTotalLenth ||
  53.             prevDrag != drag ||
  54.             prevTotalWeight != totalWeght ||
  55.             prevAngularDrag != angularDrag
  56.             )
  57.         {
  58.             UpdateWire();
  59.         }
  60.  
  61.         prevTotalLenth = totalLength;
  62.         prevDrag = drag;
  63.         prevTotalWeight = totalWeght;
  64.         prevAngularDrag = angularDrag;
  65.  
  66.         if (
  67.             prevRadius != radius &&
  68.             usePhysics
  69.             )
  70.         {
  71.             UpdateRadius();
  72.         }
  73.         prevRadius = radius;
  74.  
  75.     }
  76.  
  77.     private void UpdateRadius()
  78.     {
  79.         for (int i = 0; i < segments.Length; i++)
  80.         {
  81.             SetRadiusOnSegment(segments[i], radius);
  82.         }
  83.     }
  84.  
  85.     private void SetRadiusOnSegment(Transform transform, float radius)
  86.     {
  87.         SphereCollider sphereCollider = transform.GetComponent<SphereCollider>();
  88.         sphereCollider.radius = radius;
  89.     }
  90.  
  91.     private void UpdateWire()
  92.     {
  93.         for (int i = 0; i < segments.Length; i++)
  94.         {
  95.             if (i != 0)
  96.             {
  97.                 UpdateLenthOnSegment(segments[i]);
  98.             }
  99.             UpdateWeightOnSegment(segments[i], totalWeght, drag, angularDrag);
  100.         }
  101.     }
  102.  
  103.     private void UpdateWeightOnSegment(Transform transform, float totalWeght, float drag, float angularDrag)
  104.     {
  105.         Rigidbody rigidbody = transform.GetComponent<Rigidbody>();
  106.         rigidbody.mass = totalWeght / segments.Length;
  107.         rigidbody.drag = drag;
  108.         rigidbody.angularDrag = angularDrag;
  109.     }
  110.  
  111.     private void UpdateLenthOnSegment(Transform transform)
  112.     {
  113.         ConfigurableJoint joint = transform.GetComponent<ConfigurableJoint>();
  114.         if (joint != null)
  115.         {
  116.             joint.connectedAnchor = Vector3.forward * totalLength / segments.Length;
  117.         }
  118.     }
  119.  
  120.     private void RemoveSegments()
  121.     {
  122.         for (int i = 0; i < segments.Length; i++)
  123.         {
  124.             if (segments[i] != null)
  125.             {
  126.                 Destroy(segments[i].gameObject);
  127.             }
  128.         }
  129.     }
  130.  
  131.     private void OnDrawGizmos()
  132.     {
  133.         for (int i = 0; i < segments.Length; i++)
  134.         {
  135.             if (segments[i] != null)
  136.             {
  137.                 Gizmos.DrawWireSphere(segments[i].position, radius);
  138.             }
  139.         }
  140.     }
  141.  
  142.     private void GenerateSegments()
  143.     {
  144.         /*
  145.          JoinSegment(startTransform,null,true);
  146.         Transform prevTreansform = startTransform;
  147.  
  148.         Vector3 direction = (endTransform.position - startTransform.position);
  149.  
  150.         for (int i = 0; i < segmentCount; i++)
  151.         {
  152.             GameObject segment = new GameObject($"segment_{i}");
  153.             segment.transform.SetParent(segmentParent);
  154.             segments[i] = segment.transform;
  155.  
  156.             Vector3 pos = prevTreansform.position + (direction / segmentCount);
  157.             segment.transform.position = pos;
  158.  
  159.             JoinSegment(segment.transform, prevTreansform);  
  160.  
  161.             prevTreansform = segment.transform;
  162.         }
  163.         JoinSegment(endTransform, prevTreansform, true, true);
  164.          */
  165.         Transform prevTreansform = segments[0];
  166.         Vector3 direction = (segments[segments.Length - 1].position - segments[0].position);
  167.  
  168.         //Transform prevTreansform;
  169.         for (int i = 0; i < segments.Length; i++)
  170.         {
  171.             if (i == 0)
  172.             {
  173.                 JoinSegment(segments[i].transform, null, true);
  174.             }
  175.             /*
  176.             else if (i == segments.Length - 1)
  177.             {
  178.                 JoinSegment(segments[i].transform, null, true);
  179.             }
  180.             */
  181.             else
  182.             {
  183.                 Vector3 pos = prevTreansform.position + (direction / segments.Length);
  184.                 segments[i].transform.position = pos;
  185.                 JoinSegment(segments[i].transform, prevTreansform);
  186.                 prevTreansform = segments[i];
  187.             }
  188.  
  189.             prevTreansform = segments[i].transform;
  190.         }
  191.     }
  192.  
  193.     private void JoinSegment(Transform current, Transform connectedTransform, bool isKenetic = false, bool isCloseConnected = false)
  194.     {
  195.         if (current.GetComponent<Rigidbody>() == null)
  196.         {
  197.             Rigidbody rigidbody = current.AddComponent<Rigidbody>();
  198.             rigidbody.isKinematic = isKenetic;
  199.             rigidbody.mass = totalWeght / segments.Length;
  200.             rigidbody.drag = drag;
  201.             rigidbody.angularDrag = angularDrag;
  202.         }
  203.  
  204.         if (usePhysics)
  205.         {
  206.             SphereCollider sphereCollider = current.AddComponent<SphereCollider>();
  207.             sphereCollider.radius = radius;
  208.         }
  209.  
  210.         if (connectedTransform != null)
  211.         {
  212.             ConfigurableJoint joint = current.GetComponent<ConfigurableJoint>();
  213.             if (joint == null)
  214.             {
  215.                 joint = current.AddComponent<ConfigurableJoint>();
  216.             }
  217.  
  218.             joint.connectedBody = connectedTransform.GetComponent<Rigidbody>();
  219.  
  220.             joint.autoConfigureConnectedAnchor = false;
  221.  
  222.             if (isCloseConnected)
  223.             {
  224.                 joint.connectedAnchor = Vector3.forward * 0.1f;
  225.             }
  226.             else
  227.             {
  228.                 joint.connectedAnchor = Vector3.forward * (totalLength / segments.Length);
  229.             }
  230.  
  231.             joint.xMotion = ConfigurableJointMotion.Locked;
  232.             joint.yMotion = ConfigurableJointMotion.Locked;
  233.             joint.zMotion = ConfigurableJointMotion.Locked;
  234.  
  235.             joint.angularXMotion = ConfigurableJointMotion.Free;
  236.             joint.angularYMotion = ConfigurableJointMotion.Free;
  237.             joint.angularZMotion = ConfigurableJointMotion.Locked;
  238.  
  239.             /*
  240.             joint.transform.rotation = Quaternion.Euler
  241.                 (
  242.                 joint.transform.eulerAngles.x,
  243.                 joint.transform.eulerAngles.y,
  244.                 0
  245.                 );
  246.             */
  247.  
  248.             SoftJointLimit softJointLimit = new SoftJointLimit();
  249.             softJointLimit.limit = 0;
  250.             joint.angularZLimit = softJointLimit;
  251.             /*
  252.             JointLimits jointLimits = new JointLimits();
  253.             jointLimits.max = 12;
  254.             jointLimits.min = -12;
  255.             joint.angularZMotion = lim;
  256.             */
  257.  
  258.             JointDrive jointDrive = new JointDrive();
  259.             jointDrive.positionDamper = 0;
  260.             jointDrive.positionSpring = 0;
  261.             joint.angularXDrive = jointDrive;
  262.             joint.angularYZDrive = jointDrive;
  263.         }
  264.     }
  265. }
  266.  
Tags: Unity
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement