forked from UnityTutorialsHD/Unity-Tutorial-Assets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShootAuto.cs
51 lines (46 loc) · 1.39 KB
/
ShootAuto.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
using UnityEngine;
using System.Collections;
using UnityEngine;
using System.Collections;
public class ShootAuto : MonoBehaviour
{
public Transform ballPos;
public Rigidbody Ball;
static float timeMin = 10;
static float timeMax = 15;
float forceAmount = 13000;
public GameObject effect;
void SpawnBall()
{
Rigidbody ballRigid;
if ((Ball != null) && (ballPos != null))
{
ballRigid = Instantiate(Ball, ballPos.position, ballPos.rotation) as Rigidbody;
Instantiate(effect, ballPos.position, ballPos.rotation);
if (ballRigid != null)
{
ballRigid.AddForce(ballPos.forward * forceAmount);
}
else
{
Debug.LogWarning("The instantiated item does not contain a Rigidbody");
}
}
else
{
if ((Ball == null))
{
Debug.LogWarning("Ball has not been assigned. Please assign the Ball");
}
if (ballPos == null)
{
Debug.LogWarning("ballPos has not been assigned. Please assign the ballPos");
}
}
}
void Start()
{
float timeToSpawn = Random.Range(timeMin, timeMax);
InvokeRepeating("SpawnBall", timeToSpawn, Random.Range(3, 7));
}
}