Jump to content

First project: action group toggle using collider detection


Recommended Posts

Because I'd love more ways of interaction in the kerbal Universe and I'd have a ton of applications for it, I've decided to try and make a switch. The principle is simple, if any collider interacts with the collider of the part model, it will trigger an action group and either reset or stay that way until a collider interacts again. Great for making automated machines, ignition system of a piston engine, pinball etc.

pizzaoverhead has helped with some basics and gave me the following text:
 

Spoiler

Programming languages aren't easy for anyone to begin with, but with practice and experimentation, anyone can learn enough to make a KSP mod :) Once you have the IDE set up, autocomplete shows you the possibilities for you can write, making it much easier.

While I can't write the plugin for you as I have very little free time these days, I can give you an overview of how collisions work. There are six methods used:

 

  • OnCollisionEnter
  • OnCollisionStay
  • OnCollisionExit
  • OnTriggerEnter
  • OnTriggerStay
  • OnTriggerExit

 

When you give your part module methods that are public and have any of these names (and the right parameters, see the online Unity docs for full details), the code will be called when that part module experiences a collision. You can get details of the kind of collision from the parameters. Enter is called in the physics frame that the part starts touching something, stay is called every physics frame that the part continues touching something, and exit is called on the physics frame the part stops touching the object. The trigger versions of these are used for trigger colliders, aka trigger zones. These are colliders that don't physically interact with objects, but produce collision messages as if they did. Airlock and ladder colliders are an example of these in KSP.

If you want to make a part that switches, you could create a switch model and place a trigger collider over it. OnTriggerEnter, you would move the switch and call whatever action you want it to perform (Smart Parts code for instance). To move the switch, you could fire an animation, or more simply, adjust the rotation of the switch rocker from code.

Below is the basic code to create a part module that writes collision messages to the Alt+F2 debug log:


using System;
using UnityEngine;

namespace MyMod
{
    public class ModuleCollisionPrinter : PartModule
    {
        public void OnCollisionEnter(Collision collision)
        {
            Debug.Log("OnCollisionEnter " + name);

            // Example of using aspects of the collision
            if (collision.relativeVelocity.magnitude > 5)
            {
                        // Hit too fast: Damage the switch
            }
        }

        public void OnCollisionStay(Collision collision)
        {
            Debug.Log("OnCollisionStay " + name);
        }

        private void OnCollisionExit(Collision collision)
        {
            Debug.Log("OnCollisionExit " + name);
        }

        public void OnTriggerEnter(Collider other)
        {
            Debug.Log("OnTriggerEnter " + other.transform);
            // Change switch graphics and call Smart Parts here.
        }

        public void OnTriggerStay(Collider other)
        {
            Debug.Log("OnTriggerStay " + other.transform);
        }

        public void OnTriggerExit(Collider other)
        {
            Debug.Log("OnTriggerExit " + other.transform);
        }
    }
}

 

Good luck!

- Pizzaoverhead.

Using Unity 4.2.2 I have set up Monodevelop with the correct references. Right now I'm trying to find the basics like syntax, code structure, code for manipulating action groups, calling a context menu etc. The amount of information is enormous and being a person who visualizes everything, trying to translate code into a picture or movie and never coded before, it will be a slow process.

I'll be writing about my progress and occasionally ask the good people on this forum for a little help. Any spontaneous tips and all help will be appreciated and will be mentioned when the plugin/mod is released.

Edited by Azimech
Link to comment
Share on other sites

I think I've seen a first error: 4.2.2 is nice for creating new models but not nice for coding in 1.0.5. I'll install the right version (I'll have to look it up) on my laptop instead and keep this one on my PC.

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...