Jump to content

Spawning a single part as a vessel in orbit


Recommended Posts

I can't seem to figure out what the procedure is for taking a part and spawning it as a debris object in a particular orbit. I know that debris objects are just vessels, so I'm assuming that I have to create a Vessel, add a Part to the Vessel, add an Orbit to the Vessel, and then create a ProtoVessel from that Vessel. I haven't been able to find any documentation on instantiating a new vessel though.

Any pointers as to where I should start?

Link to comment
Share on other sites

I would approach it the other way around: Create a savegame with your debris vessel, inspect its ConfigNodes and learn how to generate those ConfigNodes, then create the ProtoVessel from them and use its loading code...

Link to comment
Share on other sites

I tried the following code but it generates an exception in the chunk of code after "Set orbit params". I can't figure out what I'm doing wrong here. There has got to be a way to create a vessel dynamically.


// Find the Kerbin CelestialBody
CelestialBody kerbin = null;
foreach (CelestialBody aBody in FlightGlobals.Bodies)
{
if (aBody.name == "Kerbin")
{
kerbin = aBody;
}
}

// Create a new blank vessel
Vessel newVessel = new Vessel();

// Add a part to the vessel
Debug.Log("getPartInfoByName");
AvailablePart ap = PartLoader.getPartInfoByName("cupola");
Debug.Log("Instantiate");
UnityEngine.Object obj = UnityEngine.Object.Instantiate(ap.partPrefab);
Debug.Log("newPart");
Part newPart = (Part)obj;
Debug.Log("rootPart ; parts.Add()");
newPart.gameObject.name = "cupola";
newPart.partInfo = ap;
newVessel.rootPart = newPart;
newVessel.parts = new List<Part>();
newVessel.parts.Add(newPart);

// Set vessel parameters
Debug.Log("Set vessel params");
newVessel.Landed = false;
newVessel.Splashed = false;
newVessel.landedAt = string.Empty;
newVessel.situation = Vessel.Situations.ORBITING;
newVessel.vesselType = VesselType.Debris;

// Set up an orbit for the vessel
Debug.Log("Set orbit params");
double utTime = Planetarium.GetUniversalTime();
OrbitPhysicsManager.HoldVesselUnpack(180);
// The next line tries to put the debris somewhere around the CelestialBody
newVessel.orbit.UpdateFromStateVectors((UnityEngine.Random.onUnitSphere + UnityEngine.Random.insideUnitSphere) * 200000f, UnityEngine.Random.onUnitSphere * 5000f, kerbin, utTime);
newVessel.orbit.Init();
newVessel.orbit.UpdateFromUT(utTime);
newVessel.orbitDriver.pos = newVessel.orbit.pos.xzy;
newVessel.orbitDriver.vel = newVessel.orbit.vel;

// Set rotation parameters
newVessel.SetRotation(UnityEngine.Random.rotation);
newVessel.angularMomentum = Vector3.zero;
newVessel.angularVelocity = Vector3.zero;

// ???
Debug.Log("GoOnRails");
newVessel.GoOnRails();

Output:


Set orbit params
[Exception]: NullReferenceException: Object reference not set to an instance of an object

Link to comment
Share on other sites

  • 2 weeks later...
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...