Tutorial – Vehicle Shooter with UFPS

In this video, I am covering adding the shooting component to a vehicle. Theoretically, you could add UFPS shooting to any object and let the player control it. Here, I’m adding the functionality to vehicles.

Extreme Vehicle Pack
Realistic Car Controller
Panzer
UFPS
Cartoon FX Pack

Vehicle Shooter Script

using UnityEngine;
using System.Collections;
/* - - - - - NOTES - RedHawk - - - - - 
 - Add this Script to an object you want to make a weapon. I suggest adding this to a Child of the Weapon itself,
 so that you can Enable and Disable the Child object (to make it inactive/disappear). I like to add arms.
 
 - Create Empty Child game objects to this weapon
 - Projectile Spawn - This is your Projectile Spawn Location. Z is the direction of fire
 - Muzzle Flash Spawn - This is the location of your Muzzle Flash. You can always use the Projectile Spawn Location
 - Shell Spawn - This is where the shell ejects from. Z or Blue Arrow is the direction the shell goes so point 
 it 90 degrees for instance.
 - Camera - create an Empty child, add an Audio Listener, Add a Camera component. Adjust the position so that
 it points as you are controlling the gun.

 - I like to add a vp_DamageHandler to the Parent object so that it can be destroyed. If you do this, make sure your 
 Projectile Spawn point is beyond your Parent object collider so you don't destroy yourself.

 - Add a vp_Respawner to the Parent Object. If you die while driving, you will never respawn unless you add a Respawner 
 script to the parent

 - Probably want to add a Unity\Standard Assets\Utility\SimpleMouseRotator on your rotation point (turret, gun, whatever). 
 For instance, your gun might go Up and Down, and your turret might go left and right.
 - You will probably want to create a script to disable the Unity\Standard Assets\Utility\SimpleMouseRotator for
 the times you are not in the vehicle
*/
namespace red{//I always create my own namespace to keep my scripts organized

 [RequireComponent(typeof(vp_Shooter))]
 public class VehicleShooter : MonoBehaviour {

 vp_Shooter myShooter=null;
 [Tooltip("This is the location your Projectile will fire from. Z (blue arrow) is forward.")]
 public GameObject projectileSpawn=null;
 [Tooltip("This is the location for your MuzzleFlash.")]
 public Transform muzzleFlashSpawn=null;
 [Tooltip("Shell Eject Location - Z is forward, so adjust accordingly.")]
 public Transform shellSpawn=null;
 public string inputFireCommand="Attack";

 void Start () {
 myShooter=this.GetComponent<vp_Shooter>();
 myShooter.m_ProjectileSpawnPoint = projectileSpawn;
 myShooter.m_MuzzleFlashSpawnPoint = muzzleFlashSpawn;
 myShooter.m_ShellEjectSpawnPoint = shellSpawn;
 }

 void FixedUpdate () {
 if(vp_Input.GetButton(inputFireCommand)){//If the Attack button is pressed as defined by the UFPS Input
 myShooter.TryFire();
 }
 }
 }
}

In the Vehicle Script

using UnityEngine;
using System.Collections;
using red;
using UnityStandardAssets.Utility;

namespace red{
 public class InTheVehicle : MonoBehaviour {

 VehicleShooter myShooter=null;
 public bool inVehicle=false;//Temporary for Testing Only
 public SimpleMouseRotator[] mouseRotators=null;
 [Tooltip("Check this box if you want to disable the Shooter GameObject instead of the vp_Shooter script component.")]
 public bool disableShooterGameObject=false;
 // Use this for initialization
 void Start () {
 if(this.GetComponentInChildren<VehicleShooter>()!=null)
 myShooter=this.GetComponentInChildren<VehicleShooter>();
 else 
 Debug.Log("You forgot to add the VehicleShooter to this vehicle. If you don't have a VehicleShooter, then remove this script",gameObject);
 }
 //REMOVE This FixedUpdate if you are calling this from the EnterExit script
 void FixedUpdate () {//For Testing ONLY
 if(inVehicle==true)
 CanShoot();//Its probably better to call this function from your EnterExit vehicle script
 if(inVehicle==false)
 NoShoot();//Its probably better to call this function from your EnterExit vehicle script

 }
 //Its probably better to call this function from your EnterExit vehicle script
 public void CanShoot(){//This is where we enable stuff
 if(disableShooterGameObject==true)
 myShooter.gameObject.SetActive(true);
 else
 myShooter.enabled=true;
 for(int i = 0; i < mouseRotators.Length; i++){//Loop thru the objects
 mouseRotators[i].enabled=true;
 }
 }
 //Its probably better to call this function from your EnterExit vehicle script
 public void NoShoot(){//This is where we disable stuff
 if(disableShooterGameObject==true)
 myShooter.gameObject.SetActive(false);
 else
 myShooter.enabled=false;
 for(int i = 0; i < mouseRotators.Length; i++){//Loop thru the objects
 mouseRotators[i].enabled=false;
 }
 }
 }
}

BONUS – Animate the Track Texture

using UnityEngine;
using System.Collections;

namespace red{
 public class AnimateTrackTexture : MonoBehaviour {

 public Material myMat=null;
 RCCCarControllerV2 myController=null;
 float myMovement=0f;
 public float factor=1f;

 void Start () {
 myMat=this.GetComponent<MeshRenderer>().material;
 myController=this.GetComponentInParent<RCCCarControllerV2>();
 }
 

 void FixedUpdate () {
 myMovement=myController.speed;
 if(myController.reversing==true)
 myMat.SetTextureOffset("_MainTex",new Vector2(myMovement/factor,0));
 else
 myMat.SetTextureOffset("_MainTex",new Vector2(-myMovement/factor,0));
 }
 }
}

HeaderWP

Advertisements

2 thoughts on “Tutorial – Vehicle Shooter with UFPS”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s