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 superpentil · Jul 28, 2012 at 03:20 AM · positionanimationsanglefunctionscalls

Is there a way to keep a function from being called until the end of another?

In my game, I have reloading and animations playing, but when I shoot, it messes up the animation and appears in an awkward position/angle. Is there a way to stop the Shooting function (and some others like it) until the Reload function/animation is done?

 function Update()
 {
  if(Input.GetButtonDown("Fire1") && BulletsLeft)
  {
  if(ShootTimer == 0)
  {
  Shoot();
  ShootTimer = ShootCooler;
  }
  }
  
  if(Input.GetKeyDown(KeyCode.R))
  {
  Reload();
  }
  
  if(ShootTimer > 0)
  {
  ShootTimer -= Time.deltaTime;
  }
  
  if(ShootTimer < 0)
  {
  ShootTimer = 0;
  }
 }
 
 function Shoot()
 {
  GameObject.Find("Assault_Rifle").animation.Play("Shoot");
  PlayShootAudio();
  var Hit : RaycastHit;
  var DirectionRay = transform.TransformDirection(Vector3.forward);
  
  Debug.DrawRay(transform.position, DirectionRay * Range, Color.blue);
  
  if(Physics.Raycast(transform.position, DirectionRay, Hit, Range))
  {
  if(Hit.rigidbody)
  {
  Hit.rigidbody.AddForceAtPosition(DirectionRay * Force, Hit.point);
  Hit.collider.SendMessageUpwards("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
  }
  }
  BulletsLeft --;
  
  if(BulletsLeft < 0)
  {
  BulletsLeft = 0;
  }
  
  if(BulletsLeft == 0)
  {
  Reload();
  }
 }
 
 function Reload()
 {
  GameObject.Find("Assault_Rifle").animation.Play("Reload");
  PlayReloadAudio();
  yield WaitForSeconds(ReloadTime);
 
  if(Clips > 0)
  {
  BulletsLeft = BulletsPerClip;
  }
  Clips --;
 }
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

2 Replies

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

Answer by Undead · Jul 28, 2012 at 07:49 AM

First off I would replace the GameObject.Find("Assault_Rifle") with a reference var to the object for performance reasons you don't want to keep using GameObject.Find, you can either add a var at the top of your script :

var assult_rifle : Assult_Rifle;

and drag and drop the object in the inspecter, then you can access it without using the costly find all the time

 assult_rifle.animation.Play("Shoot");

Or add a var at the top as above and add a find in your start or awake function to do the lookup so if you forget to assign the object it can still find it..

 function Start()
 {
 assult_rifle = GameObject.Find("Assault_Rifle");
 }

As for stopping the Shoot function the easiest way to do this for reloading and others as you mentioned is to add a boolean to the reload (or other funtion you wish to stop for the moment) so you can add an if statment to the function / functions you want to wait.

so I would add something like :

private var canShoot : boolean = true;

at the top with your vars, you can leave it a non private var if you want to access it from other scripts ofc, then change the update where you call the shoot function :

 if(Input.GetButtonDown("Fire1") && BulletsLeft)
  {
 if(ShootTimer == 0)
  {
 if(canShoot == true)
 {
  Shoot();
  ShootTimer = ShootCooler;
 }
 }
 }

and the add the boolean to your reload and other functions you wish to stop the player from shooting :

 function Reload()
 {
  GameObject.Find(&quot;Assault_Rifle&quot;).animation.Play(&quot;Reload&quot;);
  canShoot = false; // this will stop the shoot function from being called
  PlayReloadAudio();
  yield WaitForSeconds(ReloadTime);
  canShoot = true; //allows the shoot function to be called again
 if(Clips &gt; 0)
  {
  BulletsLeft = BulletsPerClip;
  }
  Clips --;
 }

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 Griffo · Jul 28, 2012 at 06:08 AM

You could try this, replace "GameObject.Find("Assault_Rifle").animation.Play("Shoot");"

 if (!animation.IsPlaying(GameObject.Find("Assault_Rifle").animation.Play("Reload"))){
         GameObject.Find("Assault_Rifle").animation.Play("Shoot");
         PlayShootAudio();
     }
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

6 People are following this question.

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

Related Questions

How to shoot raycast with slight random direction? 1 Answer

Convert vector2 mouse position into angle. 1 Answer

OnCollision change the angle of the object 1 Answer

Given starting Transform and target position, how to find full 360° angle to the target? 3 Answers

Find angle to target (not angle to rotate by) 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