Grenade.cs
1.55 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
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Grenade : MonoBehaviour {
public float speed = 1000;
Rigidbody myRigidbody;
Transform GrenadeSpawner;
bool grounded;
float livingTime;
public float power = 50.0f;
public float radius = 50.0f;
public float upForce = 0.1f;
public GameObject ExplosionPrefab;
// Use this for initialization
void Start () {
myRigidbody = GetComponent<Rigidbody>();
grounded = false;
myRigidbody.AddForce(transform.forward.normalized * speed);
}
// Update is called once per frame
void Update ()
{
livingTime += Time.deltaTime;
Invoke("Detonate",3);
Debug.Log(livingTime);
if(livingTime >= 3.3f)
{
DestroyImmediate(gameObject);
}
}
public void setSpeed(float newSpeed)
{
speed = newSpeed;
}
private void OnCollisionEnter(Collision collision)
{
grounded = true;
}
void Detonate()
{
Instantiate(ExplosionPrefab,gameObject.transform.position,gameObject.transform.rotation);
Vector3 explosionPosition = gameObject.transform.position;
Collider[] colliders = Physics.OverlapSphere(explosionPosition, radius);
foreach(Collider hit in colliders)
{
Rigidbody rb = hit.GetComponent<Rigidbody>();
if(rb!=null)
{
rb.AddExplosionForce(power, explosionPosition, radius, upForce, ForceMode.Impulse);
}
}
}
}