To make something "shoot" in Alice you can't just have a method, you must also have a specific event that runs a specific method. The most common events used for shooting are the "When the mouse is clicked on something," and the "When a key is typed," but you can use any event you want. For this example I will use the latter event. So make a "When a key is typed event" and you should get something like:
Code:
When any key is typed do nothing
Now click on the "any key" part and a drop down menu will appear. Select the button you wish to use for when you want to shoot for this example I'll use the space bar. Now you should have something like.....
Code:
When SPACE is typed do nothing
Now here comes your method. After you have created a new method let's say "shoot" drag it into the "do nothing" area of the event. Making something like:
Code:
When any key is typed do world.shoot
Now there are many different ways in which you can make a a bullet shoot an then detect if you hit something or not and collision detection isn't my strong suit. Here are some ways that I have tried in making a shooting method:
1) This method involves
actual collision between two objects the bullet and the target. It is probably the easiest but most inaccurate method. In this method you must also have created two dummy objects one being where the bullet starts and the other being where it ends it also requires you to have the bullet vehicled to the camera an that the bullet is invisible while you are not shooting. It should look kind of like:
Code:
doInOrder{
doTogether{
sphere move to endDummyObject duration .1 seconds
sphere set isShowing=true duration 0 seconds
gun turn backward .05 revolutions
}
doTogethor{
sphere move to beginDummyObject duration .1 seconds
sphere set isShowing=false duration 0 seconds
gun turn forward .05 revolutions
}
}
Now there are two things missing: Something for collision detection and something else that takes a little imagination to envision. See the bullet moves forward and then comes back like a spring and so makes it
possible to get two kills, one being on purpose the other being because you hit somebody while going backward. To counter this lets add a bool variable(true and false) to minimize the chance of this happening;this will make more sense later.You should have:
Code:
doInOrder{
doTogether{
sphere move to endDummyObject duration .1 seconds
haveShot set equal to true
sphere set isShowing=true duration 0 seconds
gun turn backward .05 revolutions
}
doTogethor{
sphere move to beginDummyObject duration .1 seconds
haveShot set equal to false
sphere set isShowing=false duration 0 seconds
gun turn forward .05 revolutions
}
Don't worry we are almost done with this method!
Finally create another event this one being the "While something is true." Right click it and change it to, "
When something
becomes true.
Now the event bar should look like this:
Code:
When any key is typed do nothing
Now click on the "any key" part and a drop down menu will appear. Select the button you wish to use for when you want to shoot for this example I'll use the space bar. Now you should have something like.....
Code:
When SPACE is typed world.shoot
When condition=none becomes true do nothing
Next go to the sphere's functions and you will see one that looks similar to:
Code:
sphere is within threshold of object
Drag this in replace of the "condition=none" to get:
Code:
When SPACE is typed world.shoot
When condition=sphere is within some distance of target becomes true do nothing
We now need to add two more things for this method to work with a proper result.One we need a death animation, but that will be up to you on how you want that, but we still need one more thing. Remember that bool variable we created above we are going to use it here. Go to the world's functions and find the one that says :
Drag it onto the condition and a little popup should appear;click true. You should now have:
Code:
When SPACE is typed world.shoot
When condition=sphere is within some distance of target and true becomes true do nothing
Finally drag your variable(my was called haveShot) in replace of true and you are done!
The final result is:
Code:
When SPACE is typed world.shoot
When condition=sphere is within some distance of target and haveShot becomes true do world.deathAnimation
Here is another method for shooting.
In this method,unlike the method above, there is no collision between objects. Now this method is slightly harder,but is noticeably more accurate. First imagine a coordinate plane, with that imagine a picture with its center lined with the origin. The picture ,for the sake of this example, is 10 units in length and 10 units in height. Now imagine another picture with coordinates of ( 0,10) thus being five units to the right of the image at the center. Then move the second image closer to the first one until they are touching or colliding. We will use a similar principle with the exception of the all annoying Z axis, as a way to shoot or primarily as a way to detect for "collision".
So import an object preferably a cone, and then right click the cone and select set point of view to and then select the camera. After right click the cone again and then select turn forward .25 revolutions, and then move the camera
very slightly backward it should look like this:
Capture.jpg
Next place your target does not really matter what it is. Here is where the fun comes in. Go to the world's function and select "Create new function" at the top. You can name it whatever you like, but for simplicity I will call it "collisionDetection." Also it is important to make this function return a bool value so you
must select the bool type in the popup menu. This function will take one object parameter and will have one local bool variable. I will call the parameter object and the variable I will call "hasCollided" I will also set it equal to false. Now drop an if statement in the function and select true. Next go back to the world's functions and drop in 4 both a and b's so that it looks like:
Code:
if(both true and true and both true and true)
I must go so I will pick up on this later.