Mobile Sniper Rifle with UFPS Mobile Add-On

Here I show how to set up a Sniper Rifle with the UFPS Mobile Add-On. I’m using UFPS 1.5 and Unity 5.0. I know that this works with Unity 4.6 as that’s where I started the process.

Prep:

– Have UFPS 1.5 loaded and UFPS Mobile Add-On
https://www.assetstore.unity3d.com/en/#!/publisher/1115?aid=1011lGwr
– Create your own Icon for a sniper scope.
– I’m using this Military Icon Kit –
https://www.assetstore.unity3d.com/en/#!/content/18931?aid=1011lGwr
– I’m using this M4A1 and ACOG Scope
https://www.assetstore.unity3d.com/en/#!/content/14233?aid=1011lGwr
– Also, go ahead and set up your Basic Arms (comes with UFPS) and Sniper Rifle as I do not go through those steps.
– Remember to create a Unit and Unit Bank for the Sniper Rifle (Mine is called M4A1 Sniper Rifle)

Place the below script on your Sniper Weapon

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

//Works with Unity 4.6 & 5.x and UFPS 1.5

namespace UFPS
{
public class SniperScriptMobile : MonoBehaviour {

[Tooltip("Assign the Default Cross Hair UI object here.")]
public GameObject DefaultCrosshairUI;
[Tooltip("Assign the Sniper Cross Hair UI object here.")]
public GameObject SniperCrosshairUI=null;

public float FieldOfViewSetting=9f;//Maxmium Zoom amount

protected vp_FPPlayerEventHandler m_Player = null;
protected vp_FPCamera m_camera=null;

float DefaultShakeSpeed;
float DefaultFOV;//Default FieldOfView that your weapon is set to

protected virtual void Awake()
{
m_Player = GameObject.FindObjectOfType(typeof(vp_FPPlayerEventHandler)) as vp_FPPlayerEventHandler; // cache the player event handler
m_camera = GameObject.FindObjectOfType(typeof(vp_FPCamera)) as vp_FPCamera; // cache the Simple Crosshair
DefaultFOV=m_camera.RenderingFieldOfView;
DefaultShakeSpeed=m_camera.ShakeSpeed;
}

void OnGUI ()
{
if(DefaultCrosshairUI!=null && SniperCrosshairUI!=null)
{
if(m_Player.Zoom.Active)
{
SniperCrosshairUI.SetActive(true);
DefaultCrosshairUI.SetActive(false);
m_camera.ShakeSpeed=0.0f; //Stops Camera From Moving
m_camera.RenderingFieldOfView=FieldOfViewSetting;//Apply our Zoom amount
}
else
{
SniperCrosshairUI.SetActive(false);
DefaultCrosshairUI.SetActive(true);
m_camera.RenderingFieldOfView=DefaultFOV;
m_camera.ShakeSpeed=DefaultShakeSpeed;

}
}
else
Debug.Log ("You must populate the DefaultCrosshairUI and SniperCrosshairUI in the Inspector!",gameObject);
}
}
}

Place the below script on your SniperCrosshair UI

using UnityEngine;
using System.Collections.Generic;

namespace UFPS
{

public class UICrossHairOverride : vp_UICrosshair {

protected override void Awake()
{
base.Awake();
}

/// 
/// Gets or sets the value of the Crosshair texture
/// 
protected virtual Texture OnValue_Crosshair
{
get { return m_Renderer.material.mainTexture; }
set {
if(m_Tracking && EnemyCrosshair != null)
{
m_Renderer.material.mainTexture = EnemyCrosshair;
}
else if(value.name == "")
m_ShowCrosshair = false;
else
{
if(!Manager.Player.Zoom.Active)
m_ShowCrosshair = true;
//BELOW LINE IS WHAT I OVERRODE - RedHawk
m_Renderer.material.mainTexture = EnemyCrosshair;
}

if(m_Tracking)
return;

// change the icon size if it's not the default
Vector3 localScale = value.name == m_CachedTextureName ? m_CachedScale : m_CachedScale * InteractIconSize;

// make the icon size bigger if no icon shown and grabbing
localScale = Manager.Player.Interactable.Get() != null && Manager.Player.Interactable.Get().GetType() == typeof(vp_Grab) && value.name == "" ? m_CachedScale * (InteractIconSize * 3) : localScale;
vp_UITween.ScaleTo(m_GameObject, vp_UITween.Hash("scale", localScale, "duration", 0, "handle", m_ScaleCrosshairHandle) );
}
}
}
}

For those of you that want a Zoom Functionality, feel free to use the below. You need to create a Slider as a child of the SniperCrosshair and make sure the Thumb Button has a collider. Also, I set my slider up for 5 steps. If you don’t know how to do this, watch the NGUI tutorials on their website.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

//Works with Unity 4.6 & 5.x and UFPS 1.5
//If you want to use the Zoom Functionality, you need to add an NGUI slider as a child of your SniperCrosshair

namespace UFPS
{
	public class SniperScriptMobile : MonoBehaviour {
		
		[Tooltip("Assign the Default Cross Hair UI object here.")]
		public GameObject DefaultCrosshairUI;
		[Tooltip("Assign the Sniper Cross Hair UI object here.")]
		public GameObject SniperCrosshairUI=null;
		
		public float FieldOfViewSetting=9f;//Maxmium Zoom amount
		
		protected vp_FPPlayerEventHandler m_Player = null;
		protected vp_FPCamera m_camera=null;
		
		float DefaultShakeSpeed;
		float DefaultFOV;//Default FieldOfView that your weapon is set to
		public UISlider childSlider;
		float childSliderValue;
		float tempFOV;//Temporary Field Of View
		bool canZoom=false;//If we don't have a slider zoom, then we can't zoom in
		Camera childCamera;//Used when Zooming In
		
		protected virtual void Awake()
		{
			SniperCrosshairUI.SetActive(true);
			m_Player = GameObject.FindObjectOfType(typeof(vp_FPPlayerEventHandler)) as vp_FPPlayerEventHandler; // cache the player event handler
			m_camera = GameObject.FindObjectOfType(typeof(vp_FPCamera)) as vp_FPCamera; // cache the Simple Crosshair
			childCamera = m_camera.gameObject.GetComponent();
			DefaultFOV=m_camera.RenderingFieldOfView;
			DefaultShakeSpeed=m_camera.ShakeSpeed;
			if(SniperCrosshairUI.gameObject.GetComponentInChildren()!=null)
			{
				childSlider=SniperCrosshairUI.gameObject.GetComponentInChildren();
				childSliderValue=childSlider.value;
				canZoom=true;
			}
			SniperCrosshairUI.SetActive(false);
			tempFOV=FieldOfViewSetting;//Set starting tempFOV value
		}
		
		void OnGUI ()
		{
			if(DefaultCrosshairUI!=null && SniperCrosshairUI!=null)
			{
				if(m_Player.Zoom.Active)
				{
					SniperCrosshairUI.SetActive(true);
					DefaultCrosshairUI.SetActive(false);
					m_camera.ShakeSpeed=0.0f; //Stops Camera From Moving
					m_camera.RenderingFieldOfView=tempFOV;//Apply our Zoom amount
				}
				else
				{
					SniperCrosshairUI.SetActive(false);
					DefaultCrosshairUI.SetActive(true);
					m_camera.RenderingFieldOfView=DefaultFOV;
					m_camera.ShakeSpeed=DefaultShakeSpeed;
					
				}
			}
			else
				Debug.Log ("You must populate the DefaultCrosshairUI and SniperCrosshairUI in the Inspector!",gameObject);
		}
		public void ZoomInOut()
		{
			//inputValue with 5 steps equals 0, .25, .5, .75, 1
			if(canZoom)
			{
				childSliderValue=childSlider.value;
				tempFOV=Mathf.Clamp(childSliderValue*2*FieldOfViewSetting,FieldOfViewSetting/3,FieldOfViewSetting*2);
				childCamera.fieldOfView=tempFOV;
				//Debug.Log("FOV Value is "+m_camera.RenderingFieldOfView);//Shows the actual zoom
			}
		}

	}//End of Class
}//End of Namespace

HeaderWP

Advertisements

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