Bonus Eventus

Bonus Tips : A Random Guide to PartModules

Recommended Posts

Bonus Tips : A Random Guide to PartModules

A year ago I started to delve deeper into C# and Unity's API as a hobbyist programer. I've learned a lot since then and now I'm giving that information to you, in no particular order.

Random Tip #1 - KSP PartModules are Unity Game Object Components
Any class in C# that inherits from the Unity Monobehaviour class is a Game Object component. This is more a conceptual cornerstone rather than a practical tip. KSP's API already
has a bunch of ways to get at its many different classes and structs, but keep this in mind. You can always use Unity's vanilla methods regarding component Adding, Subtracting,
Destroying, Instantiating, and the like regarding your own PartModule class as well as other KSP Classes which have inherited from Monobehaviour.

Random Tip #2 - Don't Forget About Unity
KSP's API is built on top of the Unity API. Unity has pretty vast documentation and StackOverflow style Unity Answers. Don't forget to use these resources. Most of the progress I've
made modding KSP has come from going through Unity tutorials on Youtube.

Random Tip #3 - Don't Listen to Anyone Older than One 
Well, maybe listen, but certainly don't blindly trust. There is a lot of information out there about Unity and a good amount from 2011. Much of it is obsolete now that KSP is built
on Unity 5.4. For instance, did you know that Unity 5 now automatically caches Transforms? Yeah it does, when you access a Transform through a method search like Find() Unity will 
automatically cache that transform in memory. However in 2011 you had to assign the Transform to a variable within your class if you wanted to keep that Transform available in

memory. So, don't fully trust information older than a year.

Random Tip #4 - How to Lerp
Using lerp functions is actually very easy. Lerp or linear interpolation is a way to return the incremental value between two bounds at a given time assuming a constant speed. Think
of an animation clip. It has a frame at the beginning, Frame 0, and a frame at the end, Frame 60. Frames 0 through 60 represent 61 units of time. Now imagine we want to animate

the position of a game object on the Y axis from 0 meters to 1 meter. We set the value of Y for the first frame to 0 (Frame 0, Y = 0) and the value of Y for the last frame at 60 to 1
(Frame 60, Y = 1). Imagine we scrub to Frame 30 in the timeline, what will the value of Y be?  Yes it's 0.5, because the value of Y is progressing in a linear fashion. So, how do we use
the lerp function. There's a couple of things to keep in mind.

  • A lerp function will only ever return a single value, it's just an equation.
  • Lerp isn't meant to be used as an easing function. It can be, but that's just a hack.
  • Because it only returns a single value at a given time, all lerp functions need to be used in a loop of some kind. Preferably in a Unity update method, like FixedUpdate().

Let's lerp: 

public Transform t = somethingWeWishToMove.transform;

public float startTime = -1f;

public bool isLerping = true;

public void FixedUpdate()
      	if(startTime == -1f) startTime = Time.time;
      	t = new Vector3(0f, (Mathf.lerp(0f,1f,startTime/Time.time))*TimeWarp.fixedDeltaTime, 0f);
      	if(Time.time >= 60f)
          	isLerping = false;
          	startTime = -1f;

That's it for today, I'll be posting more again soon. Next time we'll be delving into the Delta of Time. Till then, Happy coding :) 

Share this post

Link to post
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.

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.