Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

C# Wait x amount of seconds and how to shoot arrow both ways?

Discussion in 'Scripting' started by Trickzbunny, Mar 15, 2016.

  1. Trickzbunny

    Trickzbunny

    Joined:
    Jun 26, 2015
    Posts:
    64
    My Main Question for now is:

    I want to have a float so I can easily change/tweak the amount of seconds it takes before my bullets are shot after pressing "t". Because my Animation takes a while, but the bullets are released right away. What would I do?
    Code (CSharp):
    1.  
    2. void Update ()
    3. {
    4.         if (Input.GetKeyDown ("t"))
    5.         {
    6.             myAnim.SetBool ("IsAttacking", true);
    7.             Invoke ("StopAttacking", 0.1f);
    8.  
    9. //What can i place here to wait X amounts of seconds till the bottom part is played and shoots arrows.
    10.  
    11.             GameObject bullet01 = (GameObject)Instantiate (PlayerArrow);
    12.             bullet01.transform.position = ArrowStart.transform.position;
    13.         }
    14. }
    15.  




    2ndly.
    I can move left, right and my sprite changes its direction, however the arrows still shoot from left to right. Is there anything I can add to make that the bullets dont shoot fix to the right, but also to the left if my sprites are looking left?
    This is what i use:

    Code (CSharp):
    1.  
    2. void Update ()
    3.     {
    4.         Vector2 position = transform.position;
    5.         position = new Vector2 (position.x + speed * Time.deltaTime, position.y);
    6.         transform.position = position;
    7.  
    8.         Vector2 max = Camera.main.ViewportToWorldPoint (new Vector2 (1, 1));
    9.    
    10.         if (transform.position.x > max.x)
    11.         {
    12.             Destroy (gameObject);
    13.         }
    14.  
    15.     }
    Thank you very much!
     
  2. ShokeR0

    ShokeR0

    Joined:
    Feb 24, 2016
    Posts:
    112
    For the first question. There are 2 ways.
    One you can just call an invoke to fire. Which seem more reasonable in this case.
    The other option is to use coroutine.

    For the second question, i don't know what the code has to do with the issue you are having. But what you need to do is to create a child gameobject attach to the player and position the transform at the position the arrow will come from and use this transform pos to create the arrow
     
  3. Merman

    Merman

    Joined:
    Nov 16, 2014
    Posts:
    51
    I realize it is not quite what you asked for, but it is possible to create keyframed events on individual animations. It seems like this might be a nicer way to get your bullets to fire at the correct moment.

    EDIT: My apologies; I just realized you're working in 2D. I'm not sure if my suggestion is still possible in that case.
     
    Last edited: Mar 15, 2016
  4. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353