-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerShooting.cs
37 lines (30 loc) · 996 Bytes
/
PlayerShooting.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerShooting : MonoBehaviour
{
public GameObject bulletPrefab;
public Transform firePoint;
public float bulletForce = 20f;
public float fireRate = 0.2f;
private float fireCooldown;
private void Update()
{
fireCooldown -= Time.deltaTime;
if (Input.GetButtonDown("Fire1") && fireCooldown <= 0f)
{
Shoot();
fireCooldown = fireRate;
}
}
private void Shoot()
{
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
// Set the bullet's direction based on the firePoint's right vector
if (firePoint.right.x > 0)
rb.velocity = firePoint.right * -bulletForce; // Move right
else
rb.velocity = firePoint.right * bulletForce; // Move left
}
}