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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Having trouble with bullet direction

Discussion in 'Scripting' started by propipeproductions, Apr 25, 2022.

  1. propipeproductions

    propipeproductions

    Joined:
    Apr 11, 2022
    Posts:
    3
    I'm very new to Unity, so I might be missing something obvious here. Ill do my best to provide references to my problem.

    I have an enemy drone prefab that flies left to right and shoots at the player.

    When the drone moves too far in one direction from the player it flips its position.
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         if (transform.position.x < (playerPos.position.x - 35f) && flipCount == 0)
    4.         {
    5.             Debug.Log("Flipped X=Negative");
    6.             Invoke("FlipPosition", 0);
    7.         }
    8.  
    9.         if (transform.position.x > (playerPos.position.x + 32f) && flipCount == 0)
    10.         {
    11.             Debug.Log("Flipped X=Positive");
    12.             Invoke("FlipPosition", 0);          
    13.         }
    Code (CSharp):
    1.     public void FlipPosition()
    2.     {  
    3.         flipCount++;
    4.         transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y);
    5.         dirRight = !dirRight;                
    6.     }
    This is how the bullets are shot
    Code (CSharp):
    1.     IEnumerator Shoot()
    2.     {
    3.         hasFired = true;
    4.         yield return new WaitForSeconds(droneCoolDown);
    5.  
    6.         GameObject enemyshoot = ObjectPooler.SharedInstance.GetPooledObject("Enemy Bullet");
    7.         if (enemyshoot != null)
    8.         {
    9.             enemyshoot.SetActive(true);
    10.             enemyshoot.transform.position = droneMuzzle.transform.position;
    11.             enemyshoot.transform.rotation = droneMuzzle.transform.rotation;
    12.         }
    13.         hasFired = false;    
    14.     }
    And this is how the bullets move
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         if (!droneScript.dirRight)
    4.         {
    5.             transform.Translate(Vector2.left * fireSpeed * Time.deltaTime, Space.Self);  
    6.         }  
    7.  
    8.         if (droneScript.dirRight)
    9.         {
    10.             transform.Translate(Vector2.right * fireSpeed * Time.deltaTime, Space.Self);              
    11.         }
    12.     }
    Now generally speaking this works just fine, except when the drone is flipped while there are existing bullets in the air. They understandably get reversed in direction due to the "dirRight" bool changing.



    Im not really sure where to go with this one... Any help would be greatly appreciated. I can always provide more info if needed.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    Don't flip the whole structure.

    Flip only the visual(s) you want flipped (as child GameObjects)

    The change the direction of movement in code.

    This lets all your shooting computations remain the same.
     
  3. propipeproductions

    propipeproductions

    Joined:
    Apr 11, 2022
    Posts:
    3
    Thanks for the quick response. I've tried this a few different ways in the past. Ive tried using the SpriteRenderer.flip.x before, but it wasnt flipping my child objects (I have a gun and particle effect attached)



    Im pretty sure I could figure out how to change the particle effect to be an animated SpriteRenderer component
     
  4. Peeling

    Peeling

    Joined:
    Nov 10, 2013
    Posts:
    404
    Couple of things:
    1. Don't use 'invoke' to call a function in the same class on the same frame (unless there's some wrinkle I'm missing)
    2. You've positioned and oriented the bullet when it was fired; just move it along its own axis. There's no reason to even refer to the drone script.