Hello! I’m trying to create a wire, but when I add the sphere collider and run the game, the components of the cable I generate start to move chaotically. After I deactivate the collider, things return to normal. I have attached the code below.
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit.Transformers;
using UnityEngine.XR.Interaction.Toolkit;
public class Wire : MonoBehaviour
{
[SerializeField] Transform startTransform, endTransform;
[SerializeField] Transform segmentsParent;
[SerializeField] int totalCount = 10;
[SerializeField] int totalLength = 10;
[SerializeField] int totalWeight = 5;
[SerializeField] int drag = 1;
[SerializeField] int angularDrag = 1;
[SerializeField] bool usePhysics;
[SerializeField] float radius;
Transform[] segments = new Transform[0];
// Start is called before the first frame update
void Start()
{
segments = new Transform[totalCount];
GenerateSegments();
}
private void OnDrawGizmos()
{
for (int i = 0; i < segments.Length; i++)
{
Gizmos.DrawWireSphere(segments[i].position, 0.1f);
}
}
private void GenerateSegments()
{
JoinSegment(startTransform, null, true, false);
AddGrab(startTransform);
Transform prevSegment = startTransform;
Vector3 direction = new Vector3(Mathf.Abs(endTransform.position.x - startTransform.position.x), Mathf.Abs(endTransform.position.y - startTransform.position.y), Mathf.Abs(endTransform.position.z - startTransform.position.z));
for(int i = 0; i < totalCount; i++)
{
GameObject segment = new($"segment_{i}");
segment.transform.SetParent(segmentsParent);
Vector3 pos = prevSegment.position + direction/totalLength;
segment.transform.position = pos;
segments[i] = segment.transform;
//Mesh mesh = new Mesh();
int layerName = LayerMask.NameToLayer("Wire");
segments[i].gameObject.layer = layerName;
JoinSegment(segment.transform,prevSegment, false, false);
//AddGrab(segment.transform);
prevSegment = segment.transform;
}
JoinSegment(endTransform,prevSegment,true,true);
AddGrab(endTransform);
}
private void JoinSegment(Transform current, Transform connectedTransform, bool isKinematic = false, bool isCloseConnected = false)
{
if (usePhysics)
{
SphereCollider sphereCollider = current.AddComponent<SphereCollider>();
sphereCollider.radius = radius;
}
Rigidbody rb = current.AddComponent<Rigidbody>();
rb.isKinematic = isKinematic;
rb.mass = 1;
rb.drag = drag;
rb.angularDrag = angularDrag;
if (connectedTransform != null)
{
ConfigurableJoint 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 / totalCount);
}
joint.xMotion = ConfigurableJointMotion.Locked;
joint.yMotion = ConfigurableJointMotion.Locked;
joint.zMotion = ConfigurableJointMotion.Locked;
joint.angularXMotion = ConfigurableJointMotion.Free;
joint.angularYMotion = ConfigurableJointMotion.Free;
joint.angularZMotion = ConfigurableJointMotion.Limited;
SoftJointLimit softJointLimit = new()
{
limit = 0
};
joint.angularZLimit = softJointLimit;
JointDrive jointDrive = new()
{
positionDamper = 0,
positionSpring = 0
};
joint.angularXDrive = jointDrive;
joint.angularYZDrive = jointDrive;
}
}
private void AddGrab(Transform current)
{
XRGrabInteractable xRGrabInteractable = current.AddComponent<XRGrabInteractable>();
}
}