Closed flagmaggot closed 6 years ago
const int kPanelHeight = 70;
const string kRequiredResource = "ElectricCharge";
[KSPField]
public bool debugMode;
[KSPField]
public float ecPerSec = 5.0f;
[KSPField]
public string magnetTransformName = "magnetTransform";
[KSPField]
public float attachRange = 0.5f;
[KSPField(guiName = "Ray", guiActive = true)]
public string rayInfo;
[KSPField]
public bool drawAttachRay = true;
Vector2 scrollVector = new Vector2();
GUILayoutOption[] panelOptions = new GUILayoutOption[] { GUILayout.Height(kPanelHeight) };
GameObject magnetObject;
GameObject targetObject;
Part targetPart;
Rigidbody magnetRigidBody;
Rigidbody targetRigidBody;
ConfigurableJoint attachmentJoint;
bool magnetIsReady = false;
int layerMask = 1;
bool isConnected;
Transform magnetTransform = null;
protected void DebugLog(string message)
{
if (!debugMode)
return;
Debug.Log("[WBIMagnetController] - " + message);
}
[KSPEvent(guiActive = true, guiName = "Magnet: Off", active = true, guiActiveUnfocused = true, unfocusedRange = 40f)]
public void MagnetToggle()
{
if (targetObject == null)
DebugLog("targetObject is null");
else
DebugLog("targetObject is not null");
if (targetObject != null && isConnected == false)
{
magnetRigidBody = magnetObject.AddComponent<Rigidbody>();
magnetRigidBody.isKinematic = true;
attachmentJoint = magnetObject.AddComponent<ConfigurableJoint>();
attachmentJoint.xMotion = ConfigurableJointMotion.Locked;
attachmentJoint.yMotion = ConfigurableJointMotion.Locked;
attachmentJoint.zMotion = ConfigurableJointMotion.Locked;
attachmentJoint.angularXMotion = ConfigurableJointMotion.Locked;
attachmentJoint.angularYMotion = ConfigurableJointMotion.Locked;
attachmentJoint.angularZMotion = ConfigurableJointMotion.Locked;
attachmentJoint.connectedBody = targetRigidBody;
isConnected = true;
Events["MagnetToggle"].guiName = "Magnet: On";
DebugLog("Created attachment joint");
}
else if (isConnected == true)
{
UnityEngine.Object.Destroy(attachmentJoint.GetComponent<ConfigurableJoint>());
UnityEngine.Object.Destroy(magnetRigidBody.GetComponent<Rigidbody>());
isConnected = false;
Events["MagnetToggle"].guiName = "Magnet: Off";
}
}
public override void OnStart(PartModule.StartState state)
{
base.OnStart(state);
if (HighLogic.LoadedSceneIsFlight)
{
magnetTransform = part.FindModelTransform(magnetTransformName);
if (magnetTransform != null)
{
DebugLog("Found magnet transform");
magnetObject = magnetTransform.gameObject;
magnetIsReady = true;
}
else
{
DebugLog("Magnet transform not found");
}
}
}
void FixedUpdate()
{
if (!HighLogic.LoadedSceneIsFlight)
return;
if (magnetTransform == null)
return;
if (isConnected)
return;
if (drawAttachRay)
{
//DebugDrawer.DebugLine(magnetTransform.position, magnetTransform.position + magnetTransform.forward * attachRange, Color.red);
//DrawLine
}
RaycastHit rayCastHit;
int tempLayerMask = ~layerMask;
if (Physics.Raycast(magnetTransform.position, magnetTransform.forward, out rayCastHit, attachRange, tempLayerMask))
{
rayInfo = rayCastHit.collider.gameObject.name.ToString();
try
{
targetObject = rayCastHit.collider.gameObject;
targetRigidBody = rayCastHit.rigidbody;
}
catch (Exception ex)
{
DebugLog("Exception encountered while trying to get targetObject: " + ex.ToString());
}
}
else
{
rayInfo = "";
targetObject = null;
}
}
change layerMask to 0
int layerMask = 0;
In your CFG for the "Rotator Hand" portion I have this:
MODULE { //This module handles mesh rotation operations such as an engine nacelle pivot. name = WBIRotationController
//(Optional, defaults to Actuator) User-friendly name of the rotation controller.
rotatorName = Rotate Hand
//(Optional, defaults to Engine) Group ID for the actuator. Allows you to differentiate
//between the VTOL manager (which uses the Engine group), and, say, a robot arm manager.
groupID = Arm
//Name of the pivot z+ points down the length of the arm. See Unity model.
rotationMeshName = Hand
//Rotation axis of the pivot (x, y, z)
rotationMeshAxis = 0,0,1
//We'll use the Servo Manager to control the rotation
//(defaults to true, so we have the buttons in the right-click menu)
guiVisible = false
//We don't do symmetry with this part. This is used for engines for the most part.
canMirrorRotation = false
//How fast to rotate the pivot
rotationDegPerSec = 15
//magnetTransformName = MagnetTrigger
magnetTransformName = magnetTransform
}
[KSPEvent(guiActive = true, guiName = "Magnet: Off", active = true, guiActiveUnfocused = true, unfocusedRange = 40f)] public void MagnetToggle() { if (_target != null && connected == false) { //_joint = new ConfigurableJoint(); _rb = _hook.AddComponent();
_rb.isKinematic = true;
//_rb.mass = 0.1f;
//_rb.useGravity = false;
//_rb.constraints = RigidbodyConstraints.FreezeAll;
---PART CONFIG MODULE { name = WhateverYouHaveYourModuleAs attachNode = MagnetTrigger //<------name of the mesh you want to use as a raycast attachRange = 0.5 drawAttachRay = true }