LegitTeddyBears

TrackingProjectile

May 31st, 2017
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class TrackingProjectile : BaseProjectile {
  6.  
  7.     GameObject m_target;
  8.     private Rigidbody rb;
  9.     public float Push = 3;
  10.     GameObject m_launcher;
  11.     int m_damage;
  12.     public GameObject Explosion;
  13.  
  14.     Vector3 m_LastKnownPostion;
  15.     private void Start()
  16.     {
  17.         rb = GetComponent<Rigidbody>();
  18.     }
  19.    
  20.     // Update is called once per frame
  21.     void Update()
  22.     {
  23.         if (m_target)
  24.         {
  25.             m_LastKnownPostion = m_target.transform.position;
  26.         }
  27.         else
  28.         {
  29.             if(transform.position == m_LastKnownPostion)
  30.             {
  31.                 //Object is created where the bomb is on impact, this creates the explosion before despawning
  32.                 GameObject BoomHolder = Instantiate(Explosion, gameObject.transform.position, gameObject.transform.rotation) as GameObject;
  33.                 //Destroy the Bomb
  34.                 Destroy(gameObject);
  35.                 //Destory the BoomHolder after 5 Seconds (Make this slightly longer than your clip)
  36.                 Destroy(BoomHolder, 5.0f);  
  37.             }
  38.         }
  39.         transform.position = Vector3.MoveTowards(transform.position, m_LastKnownPostion, Speed * Time.deltaTime);
  40.         transform.LookAt(m_target.transform, m_target.transform.up);
  41.         rb.AddRelativeForce(0, 0, Push);
  42.     }
  43.     public override void FireProjectile(GameObject launcher, GameObject target, int damage, float attackSpeed)
  44.     {
  45.         if (target)
  46.         {
  47.             m_target = target;
  48.             m_LastKnownPostion = target.transform.position;
  49.             m_launcher = launcher;
  50.             m_damage = damage;
  51.         }
  52.     }
  53. //Same deal as in the base projectile we will add this code in the Message Handler portion of the tutorial
  54.     public void OnCollisionEnter(Collision other)
  55.     {/*
  56.         if (other.gameObject == m_target)
  57.         {
  58.             DamageData dmgData = new DamageData();
  59.             dmgData.damage = m_damage;
  60.  
  61.             MessageHandler msgHandler = m_target.GetComponent<MessageHandler>();
  62.  
  63.             if (msgHandler)
  64.             {
  65.                 msgHandler.GiveMessage(MessageType.DAMAGED, m_launcher, dmgData);
  66.             }
  67.         }
  68.         //Dont destroy when you come in contact with bullets otherwise do destroy
  69.         if (other.gameObject.GetComponent<BaseProjectile>() == null)
  70.             Destroy(gameObject);
  71.     */}
  72. }
Advertisement
Add Comment
Please, Sign In to add comment