Tip:
Highlight text to annotate it
X
Hello everyone, welcome to another Xenosmash tutorial
In todays tutorial we will be showing you how to create a rocket here that will have
a constant force applied to it by way of scripting
so lets take a look at what we’ll be creating today. So I’ll hit play and we have our rocket here
so if we left click it fires a rocket
lets go ahead and hit play again and see how this works.
I have this simple model here that I created in Blender
there's nothing really fancy about it as you can tell
The first thing we need to do is add a rigidbody. So with your model selected go up to component
Physics > Rigidbody. I already have a
rigidbody applied to my model here. Now we’re going to leave everything as
default, the only thing you want uncheck is “use gravity”
then once you’ve done that go ahead and go back up to component >physics
and add a constant force modifier. I have that here as well
lets go ahead and open this up. Now we’re not going to be messing around with any of the settings
in this panel, we’re going to be doing that all in scripting (Javascript)
so we have force, relative force, torque, and relative torque. Alright
so once that’s done go ahead and create a Javascript file
I already have one open here so I’m going to double click to open it and
once you’ve done that lets take a look at our script here. We have
four variables. Our first one is variable force of type vector3
and this is going to be the lift of our rocket
and we have relative torque. This is going to be the torque applied to our rocket, basically the spin
and then we have our lift speed and our turn speed
lift speed will determine how much force is going to be applied
to our rocket and the turn speed is the amount of torque applied to our rocket
or the spin. So we’re going to be working in the
function Update and we’re going to say input.getbuttondown
Fire 1 which is the left click button by default and we're going to say
constantForce.force which is this variable
up here and we’re going to say equals vector3.up
times our lift speed
now you don’t have to have a variable here
you can change this to say 10 if you want to but I included a variable that way we can
change it in the inspector panel without having to come back in here and change it
and we have our relative torque down here which is pretty much the same thing
as this one, except here we’re saying .relativeTorque equals Vector3
and right here we have our paranthesis
and this will determine which axis it will turn on. So turnSpeed
right here is in the Z axis, and just like liftSpeed
this does not need to be a variable, it can be a number
but to make things easier we made it into a variable
finally we have our audio.Play which is just going to play an audio clip that we
assigned to our model. Once that’s done go ahead and save
no we need to add the audio source
to our model. This is not required but for it is for this tutorial
so click on it (the model) and go to component > audio
Audio source and make sure you have
your audio clip. I have one right here. I downloaded this audio clip from
Soundbible.com and I’ll provide a link to it
and you just want to make sure the audio clip is in here
uncheck play on wake. Once that's done
then add the script to your rocket model
right here, I’ll go ahead and close some of these up so you can see it
alright I have my rocket script here so you can see
it has the force and the relative torque here as well. Dont need to change anything in there
the only thing you want to change is the liftSpeed and the turnSpeed
so pick numbers that you like. I'm going with 30 and 2
this will give us a nice lift and a
fairly slow turn speed
and there you go, it's much slower
depending on what type of rocket you want to shoot off you can set this up to
120 and click play
and there you go a much faster rocket
so those are the only two thing you need to change in here
the only other thing I created for this rocket is the trail renderer
I’ll go ahead and hit play so we can see it
you can see the renderer following our model. So scroll up to
component > effects > trail renderer
and I already have it set up right here so I’m going to open it up
and I left everything as default, the only things I changed
where the size. I changed the material size to 2 and I just added
the FlameD and FireSmoke which I believe are default with
the particle system materials. So go ahead and add those
and I changed the time to a .2
the reason for that is because if I change this up to a 5
you can see that the trail renderer basically stays there for about
five seconds which doesn't look very realistic. So I went ahead and
changed this to .2 so it follows our model up
the start width is at 2 and the end width is at 1
nothing else to change here, but that’s a very simple way to create a trail renderer
to attach to our model. So that’s pretty much it
You can use the constant force modifier to create stuff
that you want physics to be applied to an object
You can create an RPG rocket or anything that needs to be
shot off with
a constant force. So that's pretty much it!
I hope you enjoyed this tutorial, thanks for watching!