Game Development Reference
In-Depth Information
1. using UnityEngine;
2. using System.Collections; 3.
4. public class PhysicsShooter : MonoBehaviour { 5.
6.
//Prefabs of projectiles to shoot
7.
public GameObject[] projectils;
8.
9.
//min and max time between shots
10.
public float minTime = 1, maxTime = 6;
11.
12.
void Start () {
13.
ShootRandomly();
14.
}
15.
16.
void Update () { 17.
18.
}
19.
20.
void ShootRandomly(){
21.
//Shoot after random time
22.
float randomTime = Random.Range(minTime, maxTime);
23.
Invoke("Shoot", randomTime);
24.
}
25.
26.
void Shoot(){
27.
//Select random projectile
28.
int index = Random.Range(0, projectils.Length);
29.
GameObject prefab = projectils[index];
30.
GameObject projectile = (GameObject)Instantiate(prefab);
31.
32.
//Shoot the projectile
33.
projectile.transform.position = transform.position;
34.
projectile.rigidbody.AddForce
35.
(transform.up * 6, ForceMode.Impulse);
Search WWH ::




Custom Search