Advertisement
Guest User

TorpedoController

a guest
Jul 30th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.84 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class TorpedoController : MonoBehaviour {
  5.  
  6.     public float moveSpeed = 15;
  7.     public float selfDestruct = 400;
  8.     private float movedDistance = 0;
  9.  
  10.     public GameObject explosionPrefab;
  11.  
  12.     // Update is called once per frame
  13.     void Update () {
  14.         transform.position = transform.position + (transform.forward * moveSpeed * Time.deltaTime);
  15.  
  16.         movedDistance += moveSpeed * Time.deltaTime;
  17.  
  18.         if (movedDistance > selfDestruct) {
  19.             Destroy (gameObject);
  20.         }
  21.     }
  22.  
  23.     void OnTriggerEnter(Collider other) {
  24.         if (other.gameObject.tag == "Enemy") {
  25.             Destroy (other.gameObject);
  26.             Explode ();
  27.         }
  28.         if (other.gameObject.tag == "Ground") {
  29.             Explode ();
  30.         }
  31.     }
  32.  
  33.     void Explode() {
  34.         GameObject.Instantiate (explosionPrefab, transform.position, transform.rotation);
  35.         Destroy (gameObject);
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement