Train-of-Thought/Assets/Scripts/Weapon.cs
Gerard Gascón 862afc9b7a init
2025-04-24 16:56:52 +02:00

27 lines
996 B
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon : MonoBehaviour
{
public float bulletVelocity;
public float fireRate;
public float Damage;
public GameObject bulletPrefab;
private float lastTimeShot;
public void shoot() {
if (Time.time > lastTimeShot + fireRate) {
GetComponentInChildren<Animator>().SetTrigger("Shoot");
lastTimeShot = Time.time;
GameObject bullet = Instantiate(bulletPrefab);
bullet.transform.rotation = gameObject.transform.rotation;
bullet.transform.position = gameObject.transform.position;
Vector2 bulletDirection = bullet.transform.right;
bullet.transform.position += new Vector3(bulletDirection.x, bulletDirection.y, 0) * 1;
bullet.GetComponent<Rigidbody2D>().velocity = bulletDirection * bulletVelocity;
bullet.GetComponent<BulletScript>().damage = Damage;
}
}
}