• 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 Addyarb · Mar 17, 2014 at 11:53 PM · collidersprojectilecollider.oncollisionenter

IEnumerator workaround.. not working around.

So I had a very hard time getting an IEnumerator to work in order to delay my projectile from firing, so I decided to (try and) get clever.

My spear thrower takes about 1 second to get to the point in his animation where he "releases" the spear.

All I did was put a collider on his hand, and a collider on the spawn point of the spear projectile. This way I won't have to use any IEnumerator, the spear simply spawns once his hand hits the area that the spear releases. Later (if this works) I'll add in code that will destroy the existing spear in his hand, but this isn't working. Any suggestions? Am I doing this all wrong?

Attached: A picture of my colliders.

Attached: The script that is attached to my players hand.`using UnityEngine; using System.Collections;

/// /// This script is attached to the player and allows /// them to fire the Blaster projectile. ///

public class FireBlaster : MonoBehaviour {

 //Variables Start___________________________________

 //The blaster projectile is attached to this in the 
 //inspector


 public GameObject spear;
 public GameObject righthand;


 //Quick references.

 private Transform myTransform;

 private Transform bulletSpawn;


 //The position at which the projectile should be
 //instantiated.

 private Vector3 launchPosition = new Vector3();


 //Used to control the rate of fire.

 private float fireRate = 0;





 private float nextFire = 0;

 //Variables End_____________________________________

 void OnCollisionEnter(Collision col)
 {
     if (col.gameObject.name == "bulletSpawn") {
         nextFire = Time.time + fireRate;



         //The launch position of the projectile will be just in front
         //of the spawn point.

         launchPosition = bulletSpawn.TransformPoint (0, 0, 1.2f);








         //Create the blaster projectile at the launchPosition and tilt its angle
         //so that its horizontal using the angle eulerAngles.x + 90.

         Instantiate (spear, launchPosition, Quaternion.Euler (bulletSpawn.eulerAngles.x + 90,
             myTransform.eulerAngles.y, 0));

     }

 }



 // Use this for initialization
 void Start ()
 {



     myTransform = transform;

     bulletSpawn = myTransform.Find ("bulletSpawn");


 }

}`

Comment
Add comment · Show 5
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 ShadoX · Mar 18, 2014 at 08:30 AM 0
Share

what exactly is the problem? "destroying" the spear that's in the hand or something else? also, why would you want to destroy it , just disable and enable it.. that way you don't have to spend resources on instantiation..

avatar image Addyarb · Mar 18, 2014 at 02:50 PM 0
Share

Hi and thanks for replying!

The issue doesn't have to do with the spear in his hand, but rather the one that should be instantiated once his hand collider hits the projectile spawn collider. You see, when I used press my Fire1 button, the projectile would fire and then the animation would lag behind. Using these colliders I'm trying to instantiaTe the projectile only when his hand actually plays the "release" animation.

avatar image tobyfee · Mar 18, 2014 at 03:01 PM 0
Share

I'll be frank: I really wouldn't recommend trying to use the motion of animated object hitting a collider as a timer. There's too much to go wrong and I really think that you need to debug your initial code problem with setting a delay. You needn't necessarily use coroutines with IEnumerator, you could do something like separating out the animation so that the pre-spear-throw is one animation, and then triggering the instantiation when that animation completes.

avatar image ShadoX · Mar 18, 2014 at 03:06 PM 0
Share

or just use some timer like at http://answers.unity3d.com/questions/273094/unity3d-timer-request.html and wait before firing the spear.. or perhaps use the animations time and just compare that with the time you'd like to shoot the spear like @ http://forum.unity3d.com/threads/98805-How-to-get-animation-s-current-frame

 if (animation["attack"].time > 0.333 && < animation["attack"].time < 0.5) {
     //shoot
 }
avatar image Olgo · Mar 18, 2014 at 03:08 PM 0
Share

i had similar thoughts about the collider method, seems kinda complex and leaves too much to go wrong. Interesting method though and could have it's uses elsewhere.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Olgo · Mar 18, 2014 at 03:06 PM

I have a similar situation in my project where the character dig's earth. I dont want the "dug earth" prefab to appear as soon as the player clicks, I want it to appear once the shovel hits the ground, which is about halfway through my animation. A very simple and easy way I do this is with a CoRoutine.

just call this CoRoutine and pass in the object you want to spawn and how long you want to wait, you could pass in the time of the animation if you want it to spawn after the animation. I do animationTime * .5f so it spawns halfway through the dig animation.

 IEnumerator TimedItemSpawn (GameObject go, float timeToWait){
      yield return new WaitForSeconds(timeToWait);
      Instantiate( go );
 }
Comment
Add comment · Show 2 · 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 Addyarb · Mar 18, 2014 at 03:28 PM 0
Share

Thanks for all of the replies! I'll give IEnumerator another go, should I use it in between void start and void update? I just really can't ever seem to get the syntax or placement right when I use timers, but I'll try my best. If not I'll try the animation timer, as that seems a little more easy to wrap my head around as far as debugging.

Thanks so much for all of these thoughts, I'll post an update with what I decide to use.

avatar image Olgo · Mar 18, 2014 at 04:37 PM 0
Share

Well it wouldn't matter where you place it on your script, as Start is always only called when the game starts and Update is always called once per frame. It would matter when you call it. You want to call it in your player controls like "if (Input.Get$$anonymous$$ouseButtonDown(0)){ //fire weapon, start animation, instantiate spear etc... }"

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Prevent a game object collide against specific colliders. 1 Answer

Collision...sometimes? 0 Answers

physics.OverlapSphere colliders 1 Answer

Trajectory Prediction with Particle Systems 1 Answer

Move projectile in a specific direction 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