Jump to content

How can I add & join two parts together in the Editor?


Recommended Posts

I would like to be able to add some small separatrons to a tank in the editor.

I have the separatron model all done, thanks to a forum user.

So, I have the code to get the original part, and from that I can get the size from the prefabSize, which I use to calculate the spacing around a circle around the center

But so far, whatever I've tried to attach the parts, either the new part is somewhere far away from the current part (seems to be at 0,0,0 in the editor) or I get the strangest game crashes.

Can someone help?

Given the following:

Original part:   Part p;

this code isn't working:

//
// Get the node_attach_top node
//
bool GetTopNode(Part p)
{
	foreach (var attachNode in p.attachNodes.Where(an => an != null))
	{
		if (p.srfAttachNode != null && attachNode == p.srfAttachNode)
			continue;
		if (attachNode.id == "")
			continue;
		if (attachNode.id == "top")
		{
			topNode = attachNode;
			return true;

		}
	}
	return false;
}

//
// Create the new part
//
public Part CreatePart(string partname)
{
	AvailablePart avPart = PartLoader.getPartInfoByName(partname);

	UnityEngine.Object obj = UnityEngine.Object.Instantiate(avPart.partPrefab);
	if (!obj)
	{
		Log.Info("CreatePart(Crate) Failed to instantiate " + avPart.partPrefab.name);
		return null;
	}

	Part newPart = UnityEngine.Object.Instantiate<Part>(avPart.partPrefab);

	newPart.gameObject.SetActive(true);
	newPart.gameObject.name = avPart.name;
	newPart.partInfo = avPart;
	newPart.symMethod = SymmetryMethod.Radial;
	
	return newPart;
}

//
// Add nodeCnt parts to the parent part "p"
//
void AddParts(Part p, int nodeCnt)
{
	double radius = Math.Min(p.prefabSize.z / 2, p.prefabSize.x / 2);
	double angle = 0;

	if (GetTopNode(p) && nodeCnt > 0)
	{
	  // Add nodeCnt parts in a circle
		for (int cnt = 0; cnt < Math.Max(2, nodeCnt); cnt++)
		{

			Part newP = CreatePart("integratedSepMotor");
			Log.Info("newP prefabSize.x: " + newP.prefabSize.x.ToString() + ", z: " + newP.prefabSize.z.ToString());
			radius = Math.Min(p.prefabSize.z / 2, p.prefabSize.x / 2) - Math.Max(newP.prefabSize.x, newP.prefabSize.z);
			if (newP != null)
			{
				//newP.partTransform = topNode.nodeTransform;
				angle = ((360 / nodeCnt) / RADIAN) * cnt;
				double x = radius * Math.Cos(angle);
				double z = radius * Math.Sin(angle);
				//pos.z = pos.z + p.prefabSize.z / 2;

				pos = topNode.position;
				pos.x += (float)Math.Round(x, 4);
				pos.z += (float)Math.Round(z, 4);

				Log.Info("cnt: " + cnt.ToString() + ",  angle: " + (angle * RADIAN).ToString() + ",   x: " + pos.x.ToString() + ", z: " + pos.z.ToString());

				// multiply rotation by 180 on y axis
			   // newP.transform.localRotation *= Quaternion.Euler(0f, 0f, 180f);

				Log.Info("Adding part");
				EditorLogic.fetch.ship.Add(newP);
				p.addChild(newP);
				newP.setParent(p);
				newP.transform.parent = p.transform;

				newP.transform.localPosition = pos;

				newP.attRotation0 = newP.transform.localRotation;
				newP.attPos0 = pos + p.attPos;
				newP.onAttach(p, true);

			}
		}
		GameEvents.onEditorShipModified.Fire(EditorLogic.fetch.ship);
	}
}

 

Edited by linuxgurugamer
Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...