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 Dreave · Dec 13, 2011 at 08:11 PM · animationraycastreloading

Stop animation when no more bullets

I have a script on my gun which triggers animations on button press and another script which fires rays and reloads my gun ect. But how would I alter this so that if there are no bullets left I cannot play the shoot animation until there are, can anyone help me?

animations script

 function Start () {



animation.wrapMode = WrapMode.Loop;

animation["idle"].speed = 0.2;

animation["walk"].speed = 1;

animation["reload"].speed = 2.5;

animation["shoot"].speed = 2;

animation["shoot"].wrapMode = WrapMode.Once;

animation["reload"].wrapMode = WrapMode.Once;

animation["shoot"].layer = 1;

animation["reload"].layer = 2;

animation.Stop();

}

function Update (){

if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1)

animation.CrossFade("walk");

else

animation.CrossFade("idle");

if(Input.GetButtonDown("Fire1")){

animation.Stop();

animation.Play("shoot");

}

if(Input.GetButtonDown("r")){

animation.Stop();

animation.CrossFade("reload");

}

}

raycast, reload script

 var amountOfShots = 8;

var reloadTime = 1;

function Update (){

if(Input.GetButtonDown("Fire1")){

 Shoot();

 }

 

if(Input.GetKeyDown("r")){

 Reload();

 }

}

function Reload (){

yield WaitForSeconds(reloadTime);

amountOfShots = 8;

}

var shootSound : AudioClip;

var bloodPrefab : Transform;

var sparksPrefab : Transform;

var hit : RaycastHit;

var range = 500;

function Shoot (){

     if(amountOfShots > 0){

     amountOfShots--;

     if(shootSound){

     audio.PlayOneShot(shootSound);

 }

if(Physics.Raycast(transform.position, transform.forward, hit, range)){

 var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);



 if(hit.collider.tag == "Enemy"){

     if(bloodPrefab){

         Instantiate(bloodPrefab, hit.point, rot);

     }

         hit.collider.gameObject.SendMessage("ApplyDamage", 25, SendMessageOptions.DontRequireReceiver);

     }else{

     if(sparksPrefab){

         Instantiate(sparksPrefab, hit.point, rot);

         }

     }

 }

}

}

Comment
Add comment · Show 2
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 Tasarran · Dec 13, 2011 at 09:50 PM 0
Share

Please edit your post so that the whole of your code is in the code brackets, or nobody will read your code, and therefore, nobody will answer.

avatar image syclamoth · Dec 14, 2011 at 01:01 AM 3
Share

sscce.org. read it.

1 Reply

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

Answer by Lo0NuhtiK · Dec 14, 2011 at 12:53 AM

Well, it looks like you have two scripts that aren't talking to each other.
Animations Script plays your SHOOT animation any time FIRE1 is pressed, regardless of whether or not you have any bullets left; It actually has no idea that there's even such a thing as bullets (aka : amountOfShots) .
You can either set up a chat-line between your scripts, or merge them into one script, or cut & splice this & that to here or there, or whatever... but whatever you do, you gotta let your FIRE1 command know that if your gun is empty then you don't want it to play the shoot animation.
It's kinda a pain to read when it's not in all in a code block like @Tasarran said, and variables thrown all over the place made it kinda weird to look at also. ... If I read it wrong, I read it wrong (went cross-eyed for a moment), but that's what I could make of it anyway.

Edit:


I suppose if you want to get dirty with it and make a quick change, you could also just add a bullet counter to your animation script.

var fullClip : int = 8 ; var bulletsLeft : int = 8 ; //just make sure you always set these 2 variables to the same value as the amountOfShots variable in your other script: if you change one, change the others to match

if(Input.GetButtonDown("Fire1")){ if(bulletsLeft > 0 ){ bulletsLeft -- ; animation.Stop(); animation.Play("shoot"); } }

if(Input.GetButtonDown("r")){ animation.Stop(); animation.CrossFade("reload"); bulletsLeft = fullClip ; }

Comment
Add comment · Show 1 · 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 Dreave · Dec 14, 2011 at 05:27 PM 0
Share

Thankyou for your help, sorry about the code I couldn't get it to format correctly.

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

6 People are following this question.

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

Related Questions

adding animation dynamically by script 0 Answers

C# Check Physics.Raycast Once 0 Answers

Animation Script Help 0 Answers

How to keep an animation attached to gameobject 0 Answers

Create light shapes on plane using C# (similar to the game Closure) 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