-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbulletScript.cs
More file actions
36 lines (33 loc) · 1.15 KB
/
bulletScript.cs
File metadata and controls
36 lines (33 loc) · 1.15 KB
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class bulletScript : MonoBehaviour
{
private Vector3 mousePos;
private Camera mainCam;
private Rigidbody2D rb;
public float bulletSpeed;
private float bulletTimer;
public float timeTillDeath;
// Start is called before the first frame update
void Start()
{
mainCam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
rb = GetComponent<Rigidbody2D>();
mousePos = mainCam.ScreenToWorldPoint(Input.mousePosition);
Vector3 direction = mousePos - transform.position;
Vector3 rotation = transform.position - mousePos;
rb.velocity = new Vector2(direction.x, direction.y).normalized * bulletSpeed;
float rot = Mathf.Atan2(rotation.y, rotation.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0, 0, rot + 180);
}
// Update is called once per frame
void Update()
{
bulletTimer += Time.deltaTime;
if (bulletTimer > timeTillDeath)
{
Destroy(gameObject);
}
}
}