Project Astero 04 – Shooting Mechanics

Hi,

Today we are going to create shooting mechanics for our game,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Tut_Projectile : MonoBehaviour
{
    public float ProjectileSpeed;
    private Rigidbody2D rb2d;

    private void Start()
    {
        rb2d = GetComponent<Rigidbody2D>();
        rb2d.velocity = transform.up * ProjectileSpeed;

        Destroy(this.gameObject, 2);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Tut_Weapon : MonoBehaviour
{
    [SerializeField]
    Transform ProjectileSpawnPoint;
    [SerializeField]
    GameObject projectile;
    [SerializeField]
    float ProjectileSpeed;
    [SerializeField]
    float ShootDelay;
    float TimeToNextShoot;

    private void Start()
    {
        TimeToNextShoot = Time.time;
    }

    private void Update()
    {
        if(Input.GetButton("Fire1"))
        {
            Shoot();
        }
    }

    public void Shoot()
    {
        if (TimeToNextShoot <= Time.time)
        {
            GameObject _projectile = Instantiate(projectile, ProjectileSpawnPoint.position, ProjectileSpawnPoint.rotation);
            _projectile.GetComponent<Tut_Projectile>().ProjectileSpeed = ProjectileSpeed;
            TimeToNextShoot = Time.time + ShootDelay;
        }
        
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *