Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 beholdenspore28 · Jul 15, 2015 at 10:53 AM · animationnot workingshootreload

This animation/shooting script not working. I need some general advise on why.

The reload animation on line 100 of this code doesnt work :(

heres my script:

using UnityEngine; using System.Collections; [RequireComponent(typeof(AudioSource))] [RequireComponent(typeof(Transform))]

public class Shoot : MonoBehaviour {

 public Rigidbody Projectile;
 public Transform ProjectileSpawn;
 public float ProjectileSpeed = 900;
 private float BeenShotFor = 0;
 public float RecoilAmmount = 0.1f;
 private float RecoilToRecover = 0;
 private bool Shot = false;
 public AudioClip GunSound;
 public Transform RecoilCam;
 public Transform GunPos;
 public Transform AimLocation;
 public Transform AimReturnPos;
 public float BulletsInMag; 
 public float MaxBulletsInMag = 30;
 public int Magazines = 5;
 public float ReloadTime = 5;
 private bool Reloading = false;
 private bool Sprinting;

 void Start (){
     BulletsInMag = MaxBulletsInMag;
 }

 // Update is called once per frame
 void Update () {
     Reload ();

     if (Input.GetKey (KeyCode.LeftShift)) {
         Sprinting = true;
     } else {
         Sprinting = false;
     }

     if (Input.GetKeyDown("r")){
         Reloading = true;
     }
     if (Shot == true) {
         BeenShotFor = BeenShotFor + 1 * Time.deltaTime;
     }

     if (BeenShotFor >= 0.1){
         Shot = false;
         BeenShotFor = 0;
     }


     if (RecoilToRecover >= 0.01f){
         GunPos.Translate (Vector3.forward * RecoilToRecover * 10 * Time.deltaTime);
         GunPos.Rotate (Vector3.left * RecoilToRecover * 10 * Time.deltaTime);
         RecoilToRecover = RecoilToRecover - RecoilToRecover * 10 * Time.deltaTime;
     }

     if (Sprinting == false){
         if (Reloading == false){
             
             if (Shot == false){
                 
                 if (BulletsInMag > 0){
                     if (Input.GetButton("Fire1")){
                         Projectile = Instantiate(Projectile, ProjectileSpawn.position, ProjectileSpawn.rotation) as Rigidbody;
                         Projectile.AddForce(ProjectileSpawn.position * ProjectileSpeed);
                         Shot = true;
                         BulletsInMag--;
                         
                         if (RecoilToRecover >= 0.5f){
                             RecoilToRecover = RecoilToRecover - 0.1f;
                         }
                         
                         //Sound
                         GetComponent<AudioSource>().PlayOneShot(GunSound);
                         
                         //Recoil
                         GunPos.Translate(Vector3.back * RecoilAmmount * Time.deltaTime);
                         GunPos.Rotate(Vector3.right * RecoilAmmount * Time.deltaTime);
                         RecoilToRecover = RecoilToRecover + RecoilAmmount * Time.deltaTime;
                     }
                 }
                 
                 if (Input.GetButton("Fire2")) {
                     GunPos.position = Vector3.MoveTowards(GunPos.position, AimLocation.position, 0.3f * Time.deltaTime);
                 } else {
                     GunPos.position = Vector3.MoveTowards(GunPos.position, AimReturnPos.position, 0.3f * Time.deltaTime);
                 }
             }
         }
     }
 }

 void Reload (){

     if (Magazines > 0){
         if (Reloading == true) {
             gameObject.GetComponent<Animation> ().Play ("Reload");
             if (ReloadTime < 0){
                 ReloadTime = 5;
                 Magazines--;
                 BulletsInMag = MaxBulletsInMag;
                 Reloading = false;
             }
             ReloadTime = ReloadTime - 1 * Time.deltaTime;
         }
     }
 }

}

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

2 People are following this question.

avatar image avatar image

Related Questions

Wierd rotations in very simple reload animation 0 Answers

Mecanim restricting animations problem 0 Answers

Animation messes up if GameObject is destroyed 1 Answer

Animation Plays in the Scene but not in the Game Preview 0 Answers

Can't find Mecanim? 2 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