-
Notifications
You must be signed in to change notification settings - Fork 0
/
PeaShooter.cs
100 lines (80 loc) · 1.96 KB
/
PeaShooter.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using UnityEngine;
using System.Collections.Generic;
public class PeaShooter : MonoBehaviour {
public float shotForce;
public float refireTime;
public float curveScalar;
public int bulletPool = 2;
public GameObject prefab;
public Transform emitPoint;
float timeSinceLastShot = 0;
float curveX;
float curveY;
float totalMove;
bool check = true;
List<GameObject> bullets;
Rigidbody shotRb;
Vector3 currentPosition;
Vector3 deltaPosition;
Vector3 lastPosition;
Vector3 originPosition;
void Start()
{
bullets = new List<GameObject>();
for (int i = 0; i < bulletPool; i++)
{
GameObject obj = (GameObject)Instantiate(prefab);
obj.SetActive(false);
bullets.Add(obj);
}
}
void Update()
{
if (Input.GetButton("Fire1"))
{
if (check)
{
originPosition = Input.mousePosition;
check = false;
}
currentPosition = Input.mousePosition;
deltaPosition = currentPosition - lastPosition;
lastPosition = currentPosition;
}
if (Input.GetButtonUp("Fire1"))
{
FireGun(shotForce);
totalMove = Vector3.Distance(originPosition, lastPosition);
}
if (shotRb)
{
if (totalMove > 150f)
{
shotRb.AddForce(-deltaPosition.x * Vector3.right * curveScalar
- deltaPosition.y * Vector3.up * curveScalar);
}
}
}
void FireGun(float shotPower)
{
if (Time.time > refireTime + timeSinceLastShot)
{
for (int i = 0; i < bullets.Count; i++)
{
if (!bullets[i].activeInHierarchy)
{
bullets[i].transform.position = emitPoint.position;
bullets[i].transform.rotation = emitPoint.rotation;
bullets[i].SetActive(true);
shotRb = bullets[i].GetComponent<Rigidbody>();
shotRb.velocity = Vector3.zero;
Vector3 trajectory = transform.forward;
shotRb.AddForce(trajectory * shotPower);
check = true;
break;
}
}
timeSinceLastShot = Time.time;
}
}
}