Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by BrayanKrad03 · Nov 03, 2015 at 01:51 AM · gun script

i need help with a gun recoil script

This is the script but can not get it when you shoot a small setback to repeat each time you shoot is done. the script works 100%

using UnityEngine; using System.Collections; using UltraReal.Utilities; using UltraReal.WeaponSystem;

///
/// Basic launcher class. This would be your gun script. /// public class BasicLauncher : UltraRealLauncherBase {

 /// <summary> 
 /// Time before the next shot can be fired.
 /// </summary>
 float _nextShotTime;

 /// <summary> 
 /// keeps track of the current ammo count before a reload is requred.
 /// </summary>
 int _ammoCount;

 /// <summary> 
 /// Time delay before next shot can be fired.  A machine gun would use a really small shot delay.
 /// </summary>
 [SerializeField] 
 private float _shotDelay = 0.1f;

 /// <summary> 
 /// How long it takes to reload the wepaon.
 /// </summary>
 [SerializeField]
 private float _reloadDelay = 1f;

 /// <summary> 
 /// The amount of ammo a magazine(clip) can hold.
 /// </summary>
 [SerializeField]
 private int _magazineSize = 20;

 /// <summary> 
 /// Reference to the Transform for the shell ejector.
 /// </summary>
 [SerializeField]
 private Transform _ejectorTransform = null;

 /// <summary> 
 /// Reference to the Transform for the muzzle position.
 /// </summary>
 [SerializeField]
 private Transform _muzzleTransform = null;

 /// <summary> 
 /// Reference to the AudoClip for reloading.
 /// </summary>
 [SerializeField]
 private AudioClip _reloadSound = null;

 
 /// <summary> 
 /// Reference to the AudoClip for missfiring.
 /// </summary>
 [SerializeField]
 private AudioClip _missfireSound = null;

 /// <summary> 
 /// Force applied to ejected shells.
 /// </summary>
 [SerializeField]
 protected float _ejectorForce = 100f;

 /// <summary> 
 /// Torque applied to ejected shells.
 /// </summary>
 [SerializeField]
 protected float _ejectorTorque = 100f;

 /// <summary> 
 /// Reference to the AudioSource for the gun.  If it's null, it will create one.
 /// </summary>
 [SerializeField]
 protected AudioSource _audioSource = null;

 /// <summary> 
 /// Reference to animator for the shooting, and reloading
 /// </summary>
 [SerializeField]
 protected Animator _animator = null;

 /// <summary> 
 /// Name of the trigger in the animation controller for Firing.
 /// </summary>
 [SerializeField]
 protected string _fireAnimTriggerName = "Fire";

 /// <summary> 
 /// Name of the trigger in the animation controller for Reloading.
 /// </summary>
 [SerializeField]
 protected string _reloadAnimTriggerName = "Reload";

 /// <summary> 
 ///    Sets defaults for weapon
 /// </summary>
 protected override void OnStart ()
 {
     base.OnStart ();

     _nextShotTime = Time.time;
     _ammoCount = _magazineSize;

     if (_audioSource == null)
         _audioSource = gameObject.AddComponent<AudioSource>();
 }

 /// <summary> 
 ///    Fires the weapon
 /// </summary>
 public override void Fire ()
 {
     if (_nextShotTime <= Time.time){
         if (_ammoCount > 0 && _ammo != null) {

             if(_muzzleTransform != null)
                 _ammo.SpawnAmmo(_muzzleTransform, _ejectorTransform, _ejectorForce, _ejectorTorque, 2f, _audioSource);

             _ammoCount--;

             if (_animator != null)
                 _animator.SetTrigger(_fireAnimTriggerName);

             base.Fire ();
         }
         else
             MissFire();
         _nextShotTime = Time.time + _shotDelay;
     }
 }

 /// <summary> 
 ///    Forces a weapon missfire.
 /// </summary>
 public override void MissFire()
 {
     base.MissFire ();

     if (_missfireSound != null && _audioSource != null)
         _audioSource.PlayOneShot (_missfireSound);
 }

 /// <summary> 
 /// Reloads the weapon
 /// </summary>
 public override void Reload ()
 {
     base.Reload ();

     if (_reloadSound != null && _audioSource != null)
         _audioSource.PlayOneShot (_reloadSound);

     if (_animator != null)
         _animator.SetTrigger(_reloadAnimTriggerName);

     _nextShotTime = Time.time + _reloadDelay;
     _ammoCount = _magazineSize;
 }

} 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

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

How can I add ammo to my gun script? 0 Answers

I've been trying to make a gun script, but somehow the boolean to tell when the bullet is destroyed doesent work 1 Answer

Animation Code Problem 2 Answers

Attaching one variable to another one by inspector 0 Answers

AddForce not working on Instantiated prefab 0 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