magnet code
Closed this issue · 3 comments
GameObject _hook;
GameObject _target;
Rigidbody _rb;
Rigidbody _targetRb;
ConfigurableJoint _joint;
[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;
_joint = _hook.AddComponent<ConfigurableJoint>();
_joint.xMotion = ConfigurableJointMotion.Locked;
_joint.yMotion = ConfigurableJointMotion.Locked;
_joint.zMotion = ConfigurableJointMotion.Locked;
_joint.angularXMotion = ConfigurableJointMotion.Locked;
_joint.angularYMotion = ConfigurableJointMotion.Locked;
_joint.angularZMotion = ConfigurableJointMotion.Locked;
_joint.connectedBody = _targetRb;
connected = true;
Events["MagnetToggle"].guiName = "Magnet: On";
}
else if (connected == true)
{
UnityEngine.Object.Destroy(_joint.GetComponent<ConfigurableJoint>());
UnityEngine.Object.Destroy(_rb.GetComponent<Rigidbody>());
connected = false;
Events["MagnetToggle"].guiName = "Magnet: Off";
}
}
public override void OnStart(PartModule.StartState state)
{
base.OnStart(state);
if (HighLogic.LoadedSceneIsFlight)
{
//Debug.LogError(hookObjectName);
//_hook = transform.Search(hookObjectName).gameObject;
_hook = part.FindModelTransform(attachNode).gameObject;
isReady = true;
}
}
[KSPField(guiName = "Ray", guiActive = true)]
public string ravInfo;
//private Rigidbody hitBody = null; //current hit reference
//bool connectable;
bool connected;
//Part hitPart;
private ConfigurableJoint joint_rotate;
//DockedVesselInfo vesselRef;
Part ref_hitPart;
//String partRef;
RaycastHit hit;
public Vector3 cameraPosition; //{ get { return armTip.position + armTip.up.normalized * 0.4f; } }
public Vector3 cameraForward; //{ get { return armTip.up; } }
//public Vector3 cameraNormal; //{ get { return armTip.forward; } }
void FixedUpdate()
{
if (drawAttachRay && HighLogic.LoadedSceneIsFlight)
{
//DebugDrawer.DebugLine(cameraPosition, cameraPosition + cameraForward * attachRange, Color.red);
//DrawLine
}
cameraPosition = part.FindModelTransform(attachNode).position;
cameraForward = part.FindModelTransform(attachNode).forward;
RaycastHit hit;
int tempLayerMask = ~layerMask;
if (!connected && Physics.Raycast(cameraPosition, cameraForward, out hit, attachRange, tempLayerMask))
{
ravInfo = hit.collider.gameObject.name.ToString();
try
{
_target = hit.collider.gameObject;
_targetRb = (hit.rigidbody);
}
catch (NullReferenceException) { }
}
else
{
ravInfo = "";
_target = null;
}
}
---PART CONFIG
MODULE
{
name = WhateverYouHaveYourModuleAs
attachNode = MagnetTrigger //<------name of the mesh you want to use as a raycast
attachRange = 0.5
drawAttachRay = true
}
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
}