Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Matthew0123 · Dec 10, 2012 at 05:53 AM · javascriptshootbasic

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);
     }    
 }
Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlucardJay · Dec 11, 2012 at 06:53 AM 1
Share

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 =]

avatar image Matthew0123 · Dec 12, 2012 at 04:59 AM 0
Share

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.

avatar image AlucardJay · Dec 12, 2012 at 06:42 AM 0
Share

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/

avatar image AlucardJay · Dec 12, 2012 at 06:43 AM 0
Share

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

avatar image Matthew0123 · Dec 13, 2012 at 01:27 AM 1
Share

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.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image save · Dec 11, 2012 at 09:44 AM 0
Share

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.

avatar image Matthew0123 · Dec 13, 2012 at 01:22 AM 0
Share

Thanks it worked. I think I was just missing the Force$$anonymous$$ode.Impulse

avatar image
0

Answer by Henri20012001 · Feb 14, 2015 at 02:41 PM

Thanks so much you helped me so much for my game

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges