Hi. I’m brand new to Unity, and I’ve been trying to glean what I can from the manual and trolling other questions, but haven’t found anything that quite fits what I want to do.
In any case, I’m trying to create a pretty simple shooter game, something like Tanks http://www.mathsisfun.com/games/tanks.html but in 3d.
----------------------------What I’m Trying----------------------------------
I’m trying to use a GUI slider to set power, then use a GUI button shoot the projectile.
In my initial experiments, I had a slider script, a button script, and a script to add an impulse to a game object. What I can’t get working is projectile instantiation, and then adding an impulse when the button is pressed. The three worked together well in a different scene where I pushed a boat. Now, I dunno what’s going on.
----------------------------What I Would Like--------------------------------
I want it to look for the projectile, and if it doesn’t exist the in the scene, instantiate at the barrel. Then, when I press the GUI button, apply an impulse to the projectile according to the slider value.
Alternatively, I would be happy if I could receive button-down on the GUI button, and instantiate the projectile with an initial velocity according to the slider.
----------------------------The Issue----------------------------------------
My projectile won’t instantiate. Also, the physics of the collider are weird. I made a “floor” of a giant, squashed cube, and put a cylinder collider on the projectile. When I was getting it to instantiate earlier (though the impulse wasn’t working, because I didn’t think ahead for the null case, and instead the slider controlled the frequency instantiation) the bloody things kept falling through the cube/floor & would phase half through the floor and roll, while some would pass right through it.
----------------------------Scripts So Far------------------------------------
Slider:
#pragma strict
static var hSliderValue : float = 0.0;
function OnGUI () {
hSliderValue = GUI.HorizontalSlider (Rect (25, 150, 100, 30), hSliderValue, 0.0, 10000000.0);
GUI.Label (Rect (25, 155, 100, 30), hSliderValue.ToString());
}
Button:
#pragma strict
static var alittlepush= 0;
function OnGUI () {
if (~alittlepush) {
alittlepush=0;
}
if (GUI.Button (Rect (260,73,200,100), "I am a button")) {
print ("You clicked the button!");
alittlepush = 1;
}
}
Impulse drive:
#pragma strict
var projectile: GameObject;
function Update () {
if (I_am_a_button.alittlepush) {
var power: Vector3 = Vector3(0,0, make_a_slider.hSliderValue);
projectile.rigidbody.AddForce(power, ForceMode.Impulse);
}
}
I’ve tried something like this:
#pragma strict
var Origin : Transform;
var respawnPrefab : GameObject;
//var respawn = GameObject.Find("cannon_shell");
function Update () {
var respawn = GameObject.Find("cannon_shell");
if (respawn !=null ) {
Instantiate (respawnPrefab, Origin.transform.position, Origin.transform.rotation);
}
if (I_am_a_button.alittlepush) {
var power: Vector3 = Vector3(0,0, powerSlider.powerValue);
respawnPrefab.rigidbody.AddForce(power, ForceMode.Impulse);
}
}
Originally, I was getting an error, “Argument Exception: Find can only be called from the main thread,” so I moved the Find variable inside the Update function. For the exposed variables: I made the barrel “Origin” and the respawnPrefab “cannon_shell,” a prefab containing a 3d model of a British anti-aircraft shell I got off blender.
Now, it just flat-out refuses to Instantiate the shell.
Halp?