UFPS – Projectile Offset

This script allows you to change the offset of the projectile for a weapon.

Link to an old Grenade Launcher Video
Old Video of mine

Below is the script Projectile Offset Script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// This is a helper script to put on your UFPS weapon so that you can change the Projectile Location.
/// I personally use this for Grenade Launcers so that I don't explode.
/// </summary>

namespace RedHawk
{
    public class ProjectileOffset : MonoBehaviour
    {
        public Vector3 FirePositionOffset = new Vector3(0.0f, 0.0f, 1.0f);
        GameObject ProjectilePosition = null;
        #region Grabbing UFPS Items
        protected Transform m_FirePosition;
        protected Transform FirePosition
        {
            get
            {
                if (m_FirePosition == null)
                {
                    ProjectilePosition = new GameObject("ProjectilePosition");
                    m_FirePosition = ProjectilePosition.transform;
                    m_FirePosition.parent = Camera.main.transform;
                }
                return m_FirePosition;
            }
        }
        protected vp_WeaponShooter m_Shooter = null;
        public vp_WeaponShooter Shooter
        {
            get
            {
                if (m_Shooter == null)
                    m_Shooter = (vp_WeaponShooter)this.GetComponent(typeof(vp_WeaponShooter));
                return m_Shooter;
            }
        }
        #endregion
        #region RegisterThis //Simply registering this script with the Player Event Handler
        vp_FPPlayerEventHandler m_Player;
        void Awake()
        {
            m_Player = this.GetComponentInParent<vp_FPPlayerEventHandler>();
        }
        protected virtual void OnEnable()
        {
            if (m_Player != null)
                m_Player.Register(this);
        }
        protected virtual void OnDisable()
        {
            Destroy(ProjectilePosition);
            if (m_Player != null)
                m_Player.Unregister(this);
        }
        #endregion
        protected void OnStart_Attack()
        {
            //Debug.Log("Attacking");
            Shooter.m_ProjectileSpawnPoint = FirePosition.gameObject;
            FirePosition.localPosition = FirePositionOffset;
            FirePosition.localEulerAngles = Vector3.zero;
        }
        protected void OnStop_Attack()
        {
            //Debug.Log("Done Attacking");
            Shooter.m_ProjectileSpawnPoint = Camera.main.transform.gameObject;
        }
    }
}
Advertisements