Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using Unity.VisualScripting;
- using UnityEngine;
- public class WireGenV2 : MonoBehaviour
- {
- //[SerializeField] private Transform startTransform, endTransform;
- //[SerializeField] private int segmentCount = 10;
- [SerializeField] private float totalLength = 10;
- [SerializeField] private float radius = 0.5f;
- [SerializeField] private float totalWeght = 10;
- [SerializeField] private float drag = 1;
- [SerializeField] private float angularDrag = 1;
- [SerializeField] bool usePhysics = true;
- [SerializeField] private Transform[] segments;
- //[SerializeField] private Transform segmentParent;
- private int prevSegmentCount;
- private float prevTotalLenth;
- private float prevDrag;
- private float prevTotalWeight;
- private float prevAngularDrag;
- private float prevRadius;
- //Transform prevTreansform;
- // Start is called before the first frame update
- private void Start()
- {
- }
- // Update is called once per frame
- private void Update()
- {
- if (prevSegmentCount != segments.Length)
- {
- //RemoveSegments();
- //segments = new Transform[segments.Length];
- GenerateSegments();
- }
- prevSegmentCount = segments.Length;
- if (
- totalLength != prevTotalLenth ||
- prevDrag != drag ||
- prevTotalWeight != totalWeght ||
- prevAngularDrag != angularDrag
- )
- {
- UpdateWire();
- }
- prevTotalLenth = totalLength;
- prevDrag = drag;
- prevTotalWeight = totalWeght;
- prevAngularDrag = angularDrag;
- if (
- prevRadius != radius &&
- usePhysics
- )
- {
- UpdateRadius();
- }
- prevRadius = radius;
- }
- private void UpdateRadius()
- {
- for (int i = 0; i < segments.Length; i++)
- {
- SetRadiusOnSegment(segments[i], radius);
- }
- }
- private void SetRadiusOnSegment(Transform transform, float radius)
- {
- SphereCollider sphereCollider = transform.GetComponent<SphereCollider>();
- sphereCollider.radius = radius;
- }
- private void UpdateWire()
- {
- for (int i = 0; i < segments.Length; i++)
- {
- if (i != 0)
- {
- UpdateLenthOnSegment(segments[i]);
- }
- UpdateWeightOnSegment(segments[i], totalWeght, drag, angularDrag);
- }
- }
- private void UpdateWeightOnSegment(Transform transform, float totalWeght, float drag, float angularDrag)
- {
- Rigidbody rigidbody = transform.GetComponent<Rigidbody>();
- rigidbody.mass = totalWeght / segments.Length;
- rigidbody.drag = drag;
- rigidbody.angularDrag = angularDrag;
- }
- private void UpdateLenthOnSegment(Transform transform)
- {
- ConfigurableJoint joint = transform.GetComponent<ConfigurableJoint>();
- if (joint != null)
- {
- joint.connectedAnchor = Vector3.forward * totalLength / segments.Length;
- }
- }
- private void RemoveSegments()
- {
- for (int i = 0; i < segments.Length; i++)
- {
- if (segments[i] != null)
- {
- Destroy(segments[i].gameObject);
- }
- }
- }
- private void OnDrawGizmos()
- {
- for (int i = 0; i < segments.Length; i++)
- {
- if (segments[i] != null)
- {
- Gizmos.DrawWireSphere(segments[i].position, radius);
- }
- }
- }
- private void GenerateSegments()
- {
- /*
- JoinSegment(startTransform,null,true);
- Transform prevTreansform = startTransform;
- Vector3 direction = (endTransform.position - startTransform.position);
- for (int i = 0; i < segmentCount; i++)
- {
- GameObject segment = new GameObject($"segment_{i}");
- segment.transform.SetParent(segmentParent);
- segments[i] = segment.transform;
- Vector3 pos = prevTreansform.position + (direction / segmentCount);
- segment.transform.position = pos;
- JoinSegment(segment.transform, prevTreansform);
- prevTreansform = segment.transform;
- }
- JoinSegment(endTransform, prevTreansform, true, true);
- */
- Transform prevTreansform = segments[0];
- Vector3 direction = (segments[segments.Length - 1].position - segments[0].position);
- //Transform prevTreansform;
- for (int i = 0; i < segments.Length; i++)
- {
- if (i == 0)
- {
- JoinSegment(segments[i].transform, null, true);
- }
- /*
- else if (i == segments.Length - 1)
- {
- JoinSegment(segments[i].transform, null, true);
- }
- */
- else
- {
- Vector3 pos = prevTreansform.position + (direction / segments.Length);
- segments[i].transform.position = pos;
- JoinSegment(segments[i].transform, prevTreansform);
- prevTreansform = segments[i];
- }
- prevTreansform = segments[i].transform;
- }
- }
- private void JoinSegment(Transform current, Transform connectedTransform, bool isKenetic = false, bool isCloseConnected = false)
- {
- if (current.GetComponent<Rigidbody>() == null)
- {
- Rigidbody rigidbody = current.AddComponent<Rigidbody>();
- rigidbody.isKinematic = isKenetic;
- rigidbody.mass = totalWeght / segments.Length;
- rigidbody.drag = drag;
- rigidbody.angularDrag = angularDrag;
- }
- if (usePhysics)
- {
- SphereCollider sphereCollider = current.AddComponent<SphereCollider>();
- sphereCollider.radius = radius;
- }
- if (connectedTransform != null)
- {
- ConfigurableJoint joint = current.GetComponent<ConfigurableJoint>();
- if (joint == null)
- {
- joint = current.AddComponent<ConfigurableJoint>();
- }
- joint.connectedBody = connectedTransform.GetComponent<Rigidbody>();
- joint.autoConfigureConnectedAnchor = false;
- if (isCloseConnected)
- {
- joint.connectedAnchor = Vector3.forward * 0.1f;
- }
- else
- {
- joint.connectedAnchor = Vector3.forward * (totalLength / segments.Length);
- }
- joint.xMotion = ConfigurableJointMotion.Locked;
- joint.yMotion = ConfigurableJointMotion.Locked;
- joint.zMotion = ConfigurableJointMotion.Locked;
- joint.angularXMotion = ConfigurableJointMotion.Free;
- joint.angularYMotion = ConfigurableJointMotion.Free;
- joint.angularZMotion = ConfigurableJointMotion.Locked;
- /*
- joint.transform.rotation = Quaternion.Euler
- (
- joint.transform.eulerAngles.x,
- joint.transform.eulerAngles.y,
- 0
- );
- */
- SoftJointLimit softJointLimit = new SoftJointLimit();
- softJointLimit.limit = 0;
- joint.angularZLimit = softJointLimit;
- /*
- JointLimits jointLimits = new JointLimits();
- jointLimits.max = 12;
- jointLimits.min = -12;
- joint.angularZMotion = lim;
- */
- JointDrive jointDrive = new JointDrive();
- jointDrive.positionDamper = 0;
- jointDrive.positionSpring = 0;
- joint.angularXDrive = jointDrive;
- joint.angularYZDrive = jointDrive;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement