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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question 2D game - Shooting in the wrong direction

Discussion in '2D' started by ReinkaosRiver, Aug 4, 2020.

  1. ReinkaosRiver

    ReinkaosRiver

    Joined:
    Aug 1, 2020
    Posts:
    3
    Hello! I'm new here. New to Unity and game development. I'm making a sample 2D game and I got stucked.

    The enemy is shooting in the wrong direction. It was supposed to shoot at me obviously. This is the WeaponScript I'm using for the enemy.

    1. WeaponScript

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. /// <summary>
    4. /// Launch projectile
    5. /// </summary>
    6. public class WeaponScript : MonoBehaviour
    7. {
    8.   //--------------------------------
    9.   // 1 - Designer variables
    10.   //--------------------------------
    11.  
    12.   /// <summary>
    13.   /// Projectile prefab for shooting
    14.   /// </summary>
    15.   public Transform shotPrefab;
    16.  
    17.   /// <summary>
    18.   /// Cooldown in seconds between two shots
    19.   /// </summary>
    20.   public float shootingRate = 0.25f;
    21.  
    22.   //--------------------------------
    23.   // 2 - Cooldown
    24.   //--------------------------------
    25.  
    26.   private float shootCooldown;
    27.  
    28.   void Start()
    29.   {
    30.     shootCooldown = 0f;
    31.   }
    32.  
    33.   void Update()
    34.   {
    35.     if (shootCooldown > 0)
    36.     {
    37.       shootCooldown -= Time.deltaTime;
    38.     }
    39.   }
    40.  
    41.   //--------------------------------
    42.   // 3 - Shooting from another script
    43.   //--------------------------------
    44.  
    45.   /// <summary>
    46.   /// Create a new projectile if possible
    47.   /// </summary>
    48.   public void Attack(bool isEnemy)
    49.   {
    50.     if (CanAttack)
    51.     {
    52.       shootCooldown = shootingRate;
    53.  
    54.       // Create a new shot
    55.       var shotTransform = Instantiate(shotPrefab) as Transform;
    56.  
    57.       // Assign position
    58.       shotTransform.position = transform.position;
    59.  
    60.       // The is enemy property
    61.       ShotScript shot = shotTransform.gameObject.GetComponent<ShotScript>();
    62.       if (shot != null)
    63.       {
    64.         shot.isEnemyShot = isEnemy;
    65.       }
    66.  
    67.       // Make the weapon shot always towards it
    68.       MoveScript move = shotTransform.gameObject.GetComponent<MoveScript>();
    69.       if (move != null)
    70.       {
    71.         move.direction = this.transform.right; // towards in 2D space is the right of the sprite
    72.       }
    73.     }
    74.   }
    75.  
    76.   /// <summary>
    77.   /// Is the weapon ready to create a new projectile?
    78.   /// </summary>
    79.   public bool CanAttack
    80.   {
    81.     get
    82.     {
    83.       return shootCooldown <= 0f;
    84.     }
    85.   }
    86. }
    87.  

    2. EnemyShot ( This is the Prefab I'm using for the enemy projectile)

    It has 2 scripts:

    2a. ShotScript

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. /// <summary>
    4. /// Projectile behavior
    5. /// </summary>
    6. public class ShotScript : MonoBehaviour
    7. {
    8.   // 1 - Designer variables
    9.  
    10.   /// <summary>
    11.   /// Damage inflicted
    12.   /// </summary>
    13.   public int damage = 1;
    14.  
    15.   /// <summary>
    16.   /// Projectile damage player or enemies?
    17.   /// </summary>
    18.   public bool isEnemyShot = false;
    19.  
    20.   void Start()
    21.   {
    22.     // 2 - Limited time to live to avoid any leak
    23.     Destroy(gameObject, 20); // 20sec
    24.   }
    25. }
    26.  

    2b. MoveScript

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. /// <summary>
    4. /// Simply moves the current game object
    5. /// </summary>
    6. public class MoveScript : MonoBehaviour
    7. {
    8.     // 1 - Designer variables
    9.  
    10.     /// <summary>
    11.     /// Object speed
    12.     /// </summary>
    13.     public Vector2 speed = new Vector2(10, 10);
    14.  
    15.     /// <summary>
    16.     /// Moving direction
    17.     /// </summary>
    18.     public Vector2 direction = new Vector2(-1, 0);
    19.  
    20.     private Vector2 movement;
    21.     private Rigidbody2D rigidbodyComponent;
    22.  
    23.     void Update()
    24.     {
    25.         // 2 - Movement
    26.         movement = new Vector2(
    27.           speed.x * direction.x,
    28.           speed.y * direction.y);
    29.     }
    30.  
    31.     void FixedUpdate()
    32.     {
    33.         if (rigidbodyComponent == null) rigidbodyComponent = GetComponent<Rigidbody2D>();
    34.  
    35.         // Apply movement to the rigidbody
    36.         rigidbodyComponent.velocity = movement;
    37.     }
    38. }


    I have found a partial solution. I can rotate the Enemy Sprite to 180 degrees. If I do that the enemy shoots at me. But ofc I don't want that.

    Can somebody help me, please ? I will provide additional info. Just ask! Thank you!
     
  2. ReinkaosRiver

    ReinkaosRiver

    Joined:
    Aug 1, 2020
    Posts:
    3
    This is the example
     

    Attached Files:

  3. ReinkaosRiver

    ReinkaosRiver

    Joined:
    Aug 1, 2020
    Posts:
    3
    UPDATE: Nevermind I figure it out! Thanks everyone!