Jump to content

[SOLVED] Spawning objects


Recommended Posts

Hello and seasons greetings :)

I have an idea that will require objects (rocks, actually) to be spawned at specific locations. They'll all need to have collision and rigidybody enabled, but don't need to be parts or vessels, and would be inconvenient if marked as debris. I can see how to spawn a simple box (thanks xevilreeperx), and I think I can get the mesh imported into ksp without it being in the part menu by following the potatoroid example, but I'm unsure how I then instantiate it as a plain old object.

Could make them mini asteroids, I suppose - are there any disadvantages to doing so, and if so, what's the mechanism to spawn them?

Many thanks.

Edited by lo-fi
Link to comment
Share on other sites

You can take a look at how RealChute spawns and attaches new canopies: https://github.com/StupidChris/RealChute/blob/master/RealChute/ChuteTemplate.cs#L291

There's a bunch of fluff there, but the idea is the same. You instantiate the model from GameDatabase, position it in space wherever you want, and attach it to another transform if you want to. You can set it's speed through it's Rigidbody component.

Link to comment
Share on other sites

Thanks, that's perfect. I didn't know of a mod that does that to find source to poke through, so that'll get me going. :D

EDIT:

Excellent, I can now spawn asteroid wherever I like (which is dangerous :D)

For Future Reference, this will spawn asteroids at the position of the part the partmodule is applied to. It's for demonstration only, and not at all useful as it is:



Debug.LogWarning("RockSpawner");
GameObject test = GameDatabase.Instance.GetModel("NASAmission/Parts/PotatoRoid/PotatoRoid");
test.SetActive(true);


GameObject obj = GameObject.Instantiate(test) as GameObject;
GameObject.Destroy(test);
obj.transform.position = this.part.transform.position;

It's very rough, no error checking, and hard coded to spawn potatoroids, but it works - obviously when wrapped into a fully formed part module.

Thanks again, Chris.

Edited by lo-fi
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...