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

Make a turret rotate and shoot

Discussion in '2D' started by Desmazio, May 4, 2020.

  1. Desmazio

    Desmazio

    Joined:
    Mar 3, 2020
    Posts:
    13
    Hello, I have a quite simple problem: I wanto to make a turret to rotate and shoot. I have no problem about rotating it, but I'm having troubles to shoots the bullets in the direction on the turret. Online I've found only turrets that point the mouse or other entities, but I don't want that it points to a target.
    This is the code behind the bullet spawning:
    Code (CSharp):
    1. IEnumerator FireContinuously()
    2.     {
    3.         while(true)
    4.         {
    5.             GameObject bullet = Instantiate(bullet_prefab, cannon.transform.position, cannon.transform.rotation) as GameObject;
    6.             bullet.GetComponent<Rigidbody2D>().velocity = new Vector2(bullet_speed, 0);
    7.             yield return new WaitForSeconds(bullet_reload);
    8.         }
    9.     }
    I hope i've explained myself enough, thanks for the help.
     
  2. Nyfirex

    Nyfirex

    Joined:
    Jun 26, 2019
    Posts:
    179
    Create the turret object with all its components (rotating script, shooting script, sprite renderer, etc...) then create a child called emitter. Inside the shooting script create a transform variable (or a list if you want to use more emitters). Assign the child transform to that variable and then instantiate the bullets on the emitter (position and rotation). If the bullets aren't going the correct way, just rotate the emitter in the correct direction
     
  3. Desmazio

    Desmazio

    Joined:
    Mar 3, 2020
    Posts:
    13
    Ok I got it, what function do you suggest me to use to move the bullet?
     
  4. Nyfirex

    Nyfirex

    Joined:
    Jun 26, 2019
    Posts:
    179
    I would use rigidbody.MovePosition(). Also don't forget to create an object pooler for them
     
  5. Desmazio

    Desmazio

    Joined:
    Mar 3, 2020
    Posts:
    13
    Thank you again. But I still have some stupid problems: in this case function like MovePosition or Translate simply doesn't works, the bullet spawn in that position and they stay still.
     
  6. Nyfirex

    Nyfirex

    Joined:
    Jun 26, 2019
    Posts:
    179
    What's your code?
     
  7. Desmazio

    Desmazio

    Joined:
    Mar 3, 2020
    Posts:
    13
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MorayMouth : MonoBehaviour
    6. {
    7.  
    8.     [SerializeField] GameObject bullet_prefab;
    9.     [SerializeField] Transform target;
    10.     [SerializeField] float bullet_speed = 20f;
    11.     [SerializeField] float bullet_reload = 0.5f;
    12.     [SerializeField] float rotation_speed = 1f;
    13.  
    14.     Coroutine firing_coroutine;
    15.     bool can_shoot = true;
    16.     bool is_shooting = false;
    17.  
    18.     void Update()
    19.     {
    20.         Fire();
    21.         Rotate();
    22.     }
    23.  
    24.     private void Fire()
    25.     {
    26.         if (can_shoot && !is_shooting)
    27.         {
    28.             is_shooting = true;
    29.             firing_coroutine = StartCoroutine(FireContinuously());
    30.         }
    31.     }
    32.  
    33.     IEnumerator FireContinuously()
    34.     {
    35.         while (true)
    36.         {
    37.             GameObject bullet = Instantiate(bullet_prefab, transform.position, transform.rotation) as GameObject;
    38.             Vector3 direction = (target.transform.position - transform.position).normalized;
    39.             bullet.GetComponent<Rigidbody2D>().MovePosition(transform.position + direction * bullet_speed * Time.deltaTime);
    40.             yield return new WaitForSeconds(bullet_reload);
    41.         }
    42.     }
    43.  
    44.     private void Rotate()
    45.     {
    46.         transform.Rotate(Vector3.forward * rotation_speed * Time.deltaTime);
    47.     }
    48.  
    49. }
     
  8. Nyfirex

    Nyfirex

    Joined:
    Jun 26, 2019
    Posts:
    179
    Ok, that's not the correct way to do that. You need 2 different scripts, one for the bullet and one for the turret. Don't create a single script that handle all their behaviors. In the bullet script you code its behavior (movement, collisions, etc...), inside the turret script same thing (movement, rotation, firing, etc...). Then inside the turret script you just instantiate the bullet prefab so when it is created the bullet will start moving
     
    anycolourulike likes this.
  9. Desmazio

    Desmazio

    Joined:
    Mar 3, 2020
    Posts:
    13
    Ok, but how can I tell to the bullet script the direction I want it to move?
     
  10. Nyfirex

    Nyfirex

    Joined:
    Jun 26, 2019
    Posts:
    179
    Just move the bullet in one direction, for example up. This mean that inside MovePosition you use something like transform.up. Now whenever you create a bullet it will always go up. To give it a direction (for example where the turret is facing) you just need to rotate the emitter in the correct way. Let me summarise:
    1) Turret object with scripts that handle its behaviors (rotating, shooting, etc)
    2) Emitter child of turret object (place it in front on the turret barrel)
    3) Inside the shooting script instantiate the bullet on the emitter position and rotation
    4) Enter play mode and test it. If the bullet is going the wrong way you don't have to change the bullet script. Just rotate the emitter till the bullet goes to the correct direction
     
  11. Desmazio

    Desmazio

    Joined:
    Mar 3, 2020
    Posts:
    13
    Ok I managed to get it works. Thanks you a lot!
     
  12. Nyfirex

    Nyfirex

    Joined:
    Jun 26, 2019
    Posts:
    179
    You're welcome