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 /
This question was closed Nov 03, 2015 at 12:05 AM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by simpleone · Feb 22, 2011 at 08:31 PM · shootweapon

Weapon Script ( able to shoot )

I've been trying to add a weapon script that make's my weapon shoot when I left click on my mouse but something is wrong because my weapon doesn't shoot when I click...

anyone got a simple working script that I can use ?

Thanks!

Comment
Add comment
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

5 Replies

  • Sort: 
avatar image
6
Best Answer

Answer by SaiNagata · Jul 19, 2013 at 09:12 AM

Try This Guys Its SImple But Its Work For Me.. :3

 var Bullet : Transform;
 var Spawn : Transform;
 
 
 function Update ()
 {
     if(Input.GetButtonDown("Fire1"))
     {
         Shoot();
     }
 }
 
 function Shot()
 {
     var pel = Instantiate(Bullet, Spawn.position, Spawn.rotation);
     pel.rigidbody.AddForce(transform.forward * 8000);
     
 }
Comment
Add comment · Show 3 · 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 sandhillceltic · Jul 19, 2013 at 04:22 PM 0
Share

Line 13 should be function Shoot()

Nice code

avatar image Vanquist · Jun 01, 2015 at 12:01 PM 0
Share

when I shoot the bullet only appears it dosent move

avatar image meat5000 ♦ Vanquist · Nov 03, 2015 at 12:03 AM 0
Share

In Unity 5 you need to use GetComponent at some point to access the rigidbody.

avatar image
2

Answer by PrimeDerektive · Feb 22, 2011 at 08:37 PM

http://unity3d.com/support/documentation/ScriptReference/Object.Instantiate.html

Second example.

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
avatar image
1

Answer by Simple · Feb 22, 2011 at 08:40 PM

I used it for my dart gun:

 var gasPrefab : Transform;
 
 privatevar dart;
 var gas;
 var buttonFire_pressed : boolean;
 var ammo : int;
 
 function Update() 
 {
 
 if( Input.GetButtonDown( "Fire1" ) )
 {
     if (ammo>0)
     {
         if (audio)
         {
         audio.Play();
         audio.loop = false;
         }
         var player_camera = GameObject.Find("MainCamera");
         var ray : Ray =player_camera.camera.ViewportPointToRay(Vector3(0.5,0.5,0));   
         var hit : RaycastHit;
     //dart impact
         if (Physics.Raycast (ray, hit))
         {
             gas = Instantiate(gasPrefab, hit.point,
                         GameObject.Find("wpn_poison_2_spawn_point").transform.rotation);
             gas.name = "dart_poisongas"+Time.time;
         }
 
     }
     ammo--;
 }
 
 }
 
                 


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
avatar image
0

Answer by jdawg0212300 · Aug 08, 2015 at 06:28 PM

  var Bullet : Transform;
  var Spawn : Transform;
  
  
  function Update ()
  {
      if(Input.GetButtonDown("Fire1"))
      {
          Shoot();
      }
  }
  
  function Shot()
  {
      var pel = Instantiate(Bullet, Spawn.position, Spawn.rotation);
      pel.rigidbody.AddForce(transform.forward * 8000);
      
  }

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
avatar image
0

Answer by daniel123higgy · Sep 27, 2015 at 05:01 PM

this script also works but you have to name it "ShootDemo"

using UnityEngine; using UnityEngine; using System.Collections;

public class ShootDemo : MonoBehaviour {

 public Rigidbody projectile;
 
 public float speed = 20;
 
 
 
 // Use this for initialization
 void Start () {
     
 }
 
 // Update is called once per frame
 void Update () {
     
     if (Input.GetButtonDown("Fire1"))
     {
         Rigidbody instantiatedProjectile = Instantiate(projectile,
                                                        transform.position,
                                                        transform.rotation)
             as Rigidbody;
         
         instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));
         
     }
 }

}

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

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Need a script for a gun that shoots bullet using raycast 2 Answers

Problem with AIM to SHOOT script 1 Answer

Problem with tutorial script 2 Answers

gun switch script 26-feb-2010 1 Answer

Gun Animate when it shoots 3 Answers


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