- Home /
Simple Shooting Script
Made a simple script, I understand it all. But could someone also explain Vector2,3,4 to me also? I didn't quite understand that well, but I do know what it does.
Anyways, main issue is I try to do some_var.rigidbody.AddForce(Vector3.up*shootForce); to make it shoot forward, but it won't work. Could anyone lend me a hand please? Thanks, Code at the bottom.
function Start () {
}
var shootForce: float;
var bulletPrefab: Transform;
var myMuzzle: Transform;
function Update () {
if(Input.GetButtonDown("Fire1")){
var bullet = Instantiate(bulletPrefab,myMuzzle.transform.position,Quaternion.identity);
bullet.rigidbody.AddForce(transform.forward * shootForce * Time.deltaTime);
}
}
http://docs.unity3d.com/Documentation/ScriptReference/Vector3.html
http://docs.unity3d.com/Documentation/ScriptReference/index.Vectors.html
http://forum.unity3d.com/attachment.php?attachmentid=14159&stc=1&d=1287278424
Here are some links I strongly suggest to all new users :
Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/essential-skills/
Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/beginner/
this is the YouTube link for the above as one playlist : http://www.youtube.com/watch?v=-oXYHNSmTxg&list=PL27B696FB515608D2&feature=plcp
Character controller and terrain : http://cgcookie.com/unity/2011/12/05/introduction-to-character-controllers/
That is good to get started. Then start with a small tutorial, this is a simple 2D space shooter : http://www.unityjumpstart.com/ProofOfConcept_1/ : click on the videos part1.mp4 part2,3,4 =]
I just found another by our own Eric : http://wiki.unity3d.com/index.php?title=2DShooter : http://forum.unity3d.com/threads/7883-2D-shooter-tutorial
By then you should be getting the hang of things and starting to have ideas of your own. When you decide what kind of game you want to make, then look at each part you'll need. For example, if you want to make some terrain then walk around it with a character : http://cgcookie.com/unity/2011/12/05/introduction-to-character-controllers/
Here's another on the same 'site but I havn't checked the difficulty : http://cgcookie.com/unity/2012/02/24/game-planning-lunar-lander-01/
Basically then just search for tutorials, there are many out there, either written or on youtube.
the Unity Wiki tutorials : http://wiki.unity3d.com/index.php/Tutorials
A big list of tutorials : http://answers.unity3d.com/questions/12321/how-can-i-start-learning-unity-fast-list-of-tutori.html
Helpful page with information on using Built-In Arrays and Lists (you'll need this later!) : http://www.unifycommunity.com/wiki/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?
The unity wiki link above is very handy with lots of scripts and shaders too (just check out all the links down the left, and the tabs along the top : http://wiki.unity3d.com/index.php/Scripts )
http://answers.unity3d.com/questions/148211/list-of-frequently-asked-beginners-questions.html
http://forum.unity3d.com/threads/132628-How-to-help-the-Absolute-Beginner
If you know what kind of game you want to make, edit your question and write there what you are thinking of, then there may be a tutorial out there for that!
But just be aware, you will do lots of typing, deleting, typing, bashing head on desk, being really happy when it works and you understand how and why. I hope this helps, Happy Coding =]
Thanks Alucardj, this is mainly for a project for school and it has to do with simple stuff. Beside, I tried a lot of other tut and ways other did this and it failed. So... Just weird. Also, the ones Unity themselves wrote are pretty poor (In my Opinion) at explaining what it does, or more of a good example in a whole piece, not just a snippet.
This isn't my first time coding, but it is my first time using this engine so I need to learn some of their basics.
O$$anonymous$$, well in that case your problem is most likely
var bullet = Instantiate(bulletPrefab,my$$anonymous$$uzzle.transform.position,Quaternion.identity);
Quaternion.identity is basically a way of saying rotation of zero. Hopefully the muzzle is facing forward, so match the rotation the same way as the transform
var bullet = Instantiate(bulletPrefab,my$$anonymous$$uzzle.transform.position,my$$anonymous$$uzzle.transform.rotation);
I came from ActionScript, and also found all the Unity tutorials messy and confusing. Now I look back and can see how they are trying to demonstrate many things at once like components and scripting. I found the first 2 links very useful, the videos there are about 5 $$anonymous$$s each :
http://www.unity3dstudent.com/category/modules/essential-skills/
http://www.unity3dstudent.com/category/modules/beginner/
These are the basics you said you wanted to learn. Also, the Unity Scripting Reference is absolutely essential, just use the search box at the top-left : http://docs.unity3d.com/Documentation/ScriptReference/
Also, if you are adding force only once on instantiate (and not in the bullet script), then exactly as Save stated, use Force$$anonymous$$ode.Impulse
Yes, thanks. I will check out those links. Also yeah, I was lacking the Force.Impulse.
Yes, when ever I am trying to find a reference or a reserve word I go look in the that link!
Also, if you don't $$anonymous$$d could you help me with one more thing, I was trying to make like a gun like a assault rifle type of fire where it fire another bullet like 0.3 seconds after the other round was shot so the bullets don't all fire at once. I tried several ways, but no success.
Answer by save · Dec 11, 2012 at 09:31 AM
Vectors
Vector2, 3 and 4 are structures in Unity used to represent and contain float values within each vector. You can look at them as limited float arrays. Vector2 (X,Y) are normally used for 2D representations, this would for instance apply to textures or mouse movement. Vector3 (X, Y, Z) are normally used for representing a point in 3d. Vector4 (X, Y, Z, W) are mostly used when working with shaders and the float values of colors (RGBA).
Force
What your code currently does is to instantiate a GameObject prefab, then give the rigidbody on the instantiated GameObject a force in the forward direction of the transform the script is attached to. The problem I can see is that the force is too small for you to even notice any change to the rigidbody. As this only happens once to each bullet you would want to add an impulse force immediately with its mass calculated. Then make sure that the variable shootForce is ok in the Inspector, start on a low number (10 - 100) to make sure that you see the bullet.
bullet.rigidbody.AddForce(transform.forward * shootForce, ForceMode.Impulse);
As you don't add force to the same object over several frames you won't need to regard the delta time. Remember that if you want to add force over several frames you need to be inside FixedUpdate which is the physics time step.
What you might want to consider - depending on what your current game looks like - is raycasting ins$$anonymous$$d of actual use of physics. Rigidbodies are great at lower speeds but tend to collapse at higher, this is because the physics engine won't keep up calculating colliders making your rigidbody just $$anonymous$$r through any solid object. Raycasting on the other hand is instant, within the frame you will have the position of where the bullet will collide.
Thanks it worked. I think I was just missing the Force$$anonymous$$ode.Impulse
Your answer
Follow this Question
Related Questions
Script activate after amount of time 3 Answers
Make enemy more intelligent 0 Answers
How to shoot a puck straight? (JS) 0 Answers
Shooting with a locked camera position 1 Answer
How do I make a gun? 1 Answer