Jump to content

Using Animations for Laser Weapons


VR_Dev

Recommended Posts

Beam Sword

This is my attempt to build a custom lightsaber for my Gundam Mech, which I believe I pretty much accomplished. The beam is slightly transparent, glows, and disappears and reappears. The collider disappears as well, which means it won't bash into things when the beam is turned off.

I have yet to program the beam to cause damage to objects it comes in contact with, but it is still fun to bash things.

Gundam.jpg

AshamedConfusedBlackfly.gif

I also used the technique for a beam cannon. This animation turns on, then off after a few seconds.

Beam Cannons

JointCrazyFossa.gif

How I achieved the beam saber, the technique was kind of reversed for the cannon.

In blender:

-Create the object with the beam where it should be when toggled on. So I created the handle with the beam fully extended.

-THen I added a 1 frame LocScale animation where the beam shrinks down(really, really shrinks down) and moves just above the handle. I didn't want it to touch it because if damage ever gets added I don't want it destroying the handle.

In Unity:

-I loaded it into unity and gave the handle and beam convex mesh colliders.

-For the beam I used additive particles and played with the colors to get the pink glow I wanted.

.cfg

-Give the part and animateGeneric module and you should be set to go.

cb

Edited by clown_baby
Link to comment
Share on other sites

been trying to figure out some FX stuff myself. I think the emitter has to be in a separate mu file, with no visible mesh. when you switch the sword on, the game spawns the FX mu file at a transform you place in the sword mu file. I'm pretty sure that's how engine particles work. Hot Rocket FX mod thread has a tutorial that might help.

Link to comment
Share on other sites

I can't help in unity editor, never used it much - but I've seen a lot of editor tuts while searching for API tuts

However - I'd suggest to write a plugin anyways.

The linerenderer,emiter is well documented for scripts many examples online..

(but why do you use particles and not just a line? Isn't a light saber just a line anyways? will safe FPS compared to constently emiting that many particles to make it look like a line..)

Also damage could be done much more precise in a plugin - what ever get's touched by the sword will overheat..

currently working on star trek mod - there I've added Phasers and torpedos - in my opinion, this sword can be done wizh 2 or 3 dozens lines of code..

Link to comment
Share on other sites

been trying to figure out some FX stuff myself. I think the emitter has to be in a separate mu file, with no visible mesh. when you switch the sword on, the game spawns the FX mu file at a transform you place in the sword mu file. I'm pretty sure that's how engine particles work. Hot Rocket FX mod thread has a tutorial that might help.
Link to comment
Share on other sites

Well I guess i could give it a shot. I thought the particle sword looked cool though. Like what a plasma sword would look like

I found this which should help

private LineRenderer line = null;

protected override void onFlightStart() {
base.onFlightStart();

// First of all, create a GameObject to which LineRenderer will be attached.
GameObject obj = new GameObject( "Line" );

// Then create renderer itself...
line = obj.AddComponent< LineRenderer >();
line.transform.parent = transform; // ...child to our part...
line.useWorldSpace = false; // ...and moving along with it (rather
// than staying in fixed world coordinates)
line.transform.localPosition = Vector3.zero;
line.transform.localEulerAngles = Vector3.zero;

// Make it render a red to yellow triangle, 1 meter wide and 2 meters long
line.material = new Material( Shader.Find( "Particles/Additive" ) );
line.SetColors( Color.red, Color.yellow );
line.SetWidth( 1, 0 );
line.SetVertexCount( 2 );
line.SetPosition( 0, Vector3.zero );
line.SetPosition( 1, Vector3.forward * 2 );
}

protected override void onPartFixedUpdate() {
base.onPartFixedUpdate();
// As our part moves make the line point downwards to planet's center.
Vector3 gee = FlightGlobals.getGeeForceAtPosition( transform.position );
line.transform.rotation = Quaternion.LookRotation( gee.normalized );
}

Link to comment
Share on other sites

as far as I know, you can use these little scripts also in editor - just save it there as .CS and "drag-drop" it on your part object (at least that is what I remember from a tut I've watched once..)

Maybe that saves the full plugin - but as I said - not much clues about editor..

How about you send me your part, and I give it a try..

Although I'm sure, you can learn and grow on your own, faster than you expect - might be a funny project for you..

if you try, you can ask me what ever you don't get - I'll do my best to clarify - but I also just started with KSP coding a few weeks ago..

so don't expect me to be a university totur..

here some first hints:

>line.SetColors( Color.red, Color.yellow );

I suggest to use Color.red for both..

as the code is now, it would make a color-gradient - red at the root, yellow at the top..

you either want red or magenta on both ends..

>line.SetWidth( 1, 0 );

This will make a line like this:

1m at the root thinning out to 0 at the tip.

you probbably would want to use values like

=>line.SetWidth( 0.125, 0.08 );

This is a 12.5cm at the root thinning out to 8 cm

(depends on the scale of you model ofcourse..)

>line.SetPosition( 0, Vector3.zero );

This is the definition for the root position..

0 = first point - 1 = second point - see below

>line.SetPosition( 1, Vector3.forward * 2 );

This code line defines the end of your sword.

position 1 - see above - The Vector.forward defines what axis the line follows - the "* 2" is a shortcut to define a lenght..

the here shown definition will draw a 2m line in the "forward" direction - if your part is rotate correctly in editor, this is what you want.

maybe that 2m length is ok, maybe that is too long - depends on the size of your model..

to adjust, simply add the desired meter value there..

for example

Vector3.forward * 1.7584 wil produce a 1m 75 cm and 84mm line

I guess you get the point..

HTH

Good luck..

Link to comment
Share on other sites

as far as I know, you can use these little scripts also in editor - just save it there as .CS and "drag-drop" it on your part object (at least that is what I remember from a tut I've watched once..)

Certainly worth trying, I experimented with embedding KSP Particle in a model that I tried to turn on/off with a generic animation, didn't work out. But if embedding a line renderer works out, and you can toggle it on/off with ModuleAnimateGeneric it'd be alot easier than dealing with a plugin.

Link to comment
Share on other sites

How about you send me your part, and I give it a try..

It's really just a cylinder right now. But I could send it to you if you want. What file format?

I'm down to learn new stuff, I just taught myself blender and unity for my zoids, I just don't have a whole lot of free time right now

Link to comment
Share on other sites

It's really just a cylinder right now. But I could send it to you if you want. What file format?

I'm down to learn new stuff, I just taught myself blender and unity for my zoids, I just don't have a whole lot of free time right now

best is a KSP part..

gameData/Sword/

with model, texture and CFG

if i can load it in the game, I can work with it..

Link to comment
Share on other sites

ok I've started - it's actually allready working - but the origin seems to be off - unity is stubborn as allways..

May I ask, is this part screwed up in editor or are all axis where they should be according to docs..

Forward = z = straight up (if I'm not mistaken)

In case you don't really know, what I'm talking about, please send me the unity asset so I can check my selfe..

Link to comment
Share on other sites

ok I've started - it's actually allready working - but the origin seems to be off - unity is stubborn as allways..

May I ask, is this part screwed up in editor or are all axis where they should be according to docs..

Forward = z = straight up (if I'm not mistaken)

In case you don't really know, what I'm talking about, please send me the unity asset so I can check my selfe..

I think y is up. I'll fix it tonight

Link to comment
Share on other sites

  • 2 weeks later...

philotical has been a huge help at getting this far, but it's still slightly off base. The part and plugin source are in the updated OP if you think you have any ideas. Thanks a lot

cb

qXxn3o0.png

Link to comment
Share on other sites

Your Unity GameObject seems to be displaced slightly from the actual meshes. Make sure you zero out its position (and center the meshes again) before you export it. It looks like philotical was on the right track to figuring it out though:

swordTransform = this.part.transform;
//swordTransform = this.part.transform.Find("Cylinder.002");
//swordTransform = this.part.transform.Find("GundamBeamSaber");

So close ;.; It should work with the existing model with this little change:

swordTransform = part.FindModelTransform("Cylinder_002");

Link to comment
Share on other sites

you want to put the pivot of the grip at the cylinder's center so the beam will center on the grip.

then you can change the surface attach node in the sword config and offset it in the X by the radius of the grip so you can attach it properly to the IR hinges

damage is more complicated. you need a raycast from the grip, in the same direction and length as the linerenderer to check for collisions. if collision is detected, damage is applied to the detected object. I believe you can spawn particle FX at the point of collision as well. I'm don't know enough programming to code it though. :/

Link to comment
Share on other sites

Your Unity GameObject seems to be displaced slightly from the actual meshes. Make sure you zero out its position (and center the meshes again) before you export it. It looks like philotical was on the right track to figuring it out though:

swordTransform = this.part.transform;
//swordTransform = this.part.transform.Find("Cylinder.002");
//swordTransform = this.part.transform.Find("GundamBeamSaber");

So close ;.; It should work with the existing model with this little change:

swordTransform = part.FindModelTransform("Cylinder_002");

Its all 0s in unity

FTxmJtk.png

Link to comment
Share on other sites

Are you absolutely certain? Could you have a older export in the zip? Here's what I get with a little debug test:



var pT = DebugVisualizer.Point(part.transform, 0.25f);
var modelT = DebugVisualizer.Point(part.transform.FindChild("model"), 0.25f);
var cylT = DebugVisualizer.Point(part.FindModelTransform("Cylinder_002"), 0.25f);

pT.renderer.material = modelT.renderer.material = cylT.renderer.material = ResourceTools.ResourceUtil.LocateMaterial("DebugTools.XrayShader.shader");

pT.renderer.material.color = Color.red;
modelT.renderer.material.color = Color.blue;
cylT.renderer.material.color = Color.green;

Log.Write("Finished adding debug spheres");
Part part = FlightGlobals.ActiveVessel.parts.Find(p => p.partInfo.name == "GundamBeamSaber");

So, Part top-level GameObject in red, model transform in blue, and the point my previous snippet will find in green:

4cb5b0b306.jpg

No question that in the model provided in the zip the MeshRenderers and MeshColliders are displaced. Maybe you intended it to export this way? But then why make it radial-attachable if you only want it to attach at a single point? Hmmm

Link to comment
Share on other sites

you want to put the pivot of the grip at the cylinder's center so the beam will center on the grip.

then you can change the surface attach node in the sword config and offset it in the X by the radius of the grip so you can attach it properly to the IR hinges

damage is more complicated. you need a raycast from the grip, in the same direction and length as the linerenderer to check for collisions. if collision is detected, damage is applied to the detected object. I believe you can spawn particle FX at the point of collision as well. I'm don't know enough programming to code it though. :/

Sorry I missed this comment. I think the node_attach is to blame. I thought thats how surface attach worked, but I guess node attach can be center and surface attach will still work?

Link to comment
Share on other sites

Sorry I missed this comment. I think the node_attach is to blame. I thought thats how surface attach worked, but I guess node attach can be center and surface attach will still work?

node_attach are surface attach nodes and don't show as green ball in editor; for something like your saber grip, it would be offset in the X direction by about the radius of the object. if there is no offset the objects will clip, which may or may not work depending on your clipping settings in cfg file, Alt-F12, and collision mesh setup.

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