Advertisement
dnnkeeper

Unity Cannon fire

Mar 16th, 2013
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.96 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Shoot : MonoBehaviour
  5. {
  6.    
  7.     public Transform _thePrefab;
  8.     public int forceSpeed = 10000;
  9.     public int fireMinDelay_ms = 500;
  10.     public int fireMaxDelay_ms = 1000;
  11.     private float nextFire = 0.0f;
  12.     private float delay;
  13.      
  14.     void Start()
  15.     {
  16.     }
  17.    
  18.     void CannonFire (Transform fire_position)
  19.     {
  20.         Transform prefabInstance = (Transform) Instantiate(_thePrefab, fire_position.position,Quaternion.identity);
  21.        
  22.         prefabInstance.rigidbody.AddForce(fire_position.forward * forceSpeed);
  23.     }
  24.    
  25.     // Update is called once per frame
  26.     void Update ()
  27.     {
  28.        
  29.         if (nextFire != 0.0f && Time.time > nextFire)
  30.         {
  31.             nextFire = 0.0f;
  32.            
  33.             var fire_position = transform.Find("SpawnPoint");
  34.             CannonFire(fire_position.transform);
  35.            
  36.         }
  37.            
  38.         if (Input.GetMouseButtonDown(0) && nextFire == 0.0f)
  39.         {
  40.             delay = Random.Range(fireMinDelay_ms,fireMaxDelay_ms)/1000.0f;
  41.             nextFire = Time.time + delay;
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement