UFPS Mouse Cursor Zones

A tutorial on how to create and define mouse cursor zones for any number of menus which you may want to define. I have not found a way around utilizing the “enter” key (or the UFPS Input Manager Accept1 key). I’m not sure if Unity allows for forced input via C# scripts. The end goal is to be able to use keys 1 – 5, middle mouse button to cycle through, keys Q or E to change weapons, or a Hack that I’m working on to make it easy to swap out the same weapon type (for instance, 5 different pistols with slightly different settings for each, or sniper rifles) as a proof of concept/hack.

In the meantime, I’m going to go ahead and post this script. The defaults let me create the 5 button zone at the bottom of the screen that you see so often in many RPG games where there are items to select. I plan to allow to cycle through the choices via middle mouse button or keys 1 to 5.

Mouse Cursor Zone Helper Script – sorry, it’s a bit long

using UnityEngine;
using System.Collections;

//RedHawk - enjoy the script.  I created this for myself to help find the mouse cursor zones
//I've set this up so that you can edit this in Editor mode (non-playMode)
//Make sure that you have enough Mouse Cursor Zones on your vp_FPInput to allow for multiple menus
//This helper script will help you determine the Mouse Cursor Zone for your GUI, whether you are using Unity Gui, the new Unity GUI,
	//NGUI or something else.  To enter selection mode, you need to hit the Enter key, or whatever you have changed your Accept1 
	//input to on the UFPS Input Manager

[ExecuteInEditMode]
public class red_MouseCursorHelper : MonoBehaviour {
	[Tooltip("This will show the GUI onscreen so that you can find the correct position.")]
	public bool showGUIHelper=true;

	[Tooltip("The Value will start at 0.  Therefore if you have 3 zones on your UFPS player, zone 1 = 0, zone 2 = 1, zone 3 = 2.")]
	public int cursorZoneToUpdate=0;//This is the zone we will update with this script.

	[Tooltip("Write down all your coordinates from Play mode before you pause so you can type them in once your done.")]
	public float xCoordinate=168f;
	public float yCoordinate=550f;
	[Tooltip("How wide is your GUI Object?")]
	public float theWidth=320f;
	[Tooltip("How tall is your GUI Object?")]
	public float theHeight=60f;

	public float padding=5f;
	vp_FPInput myInput;
	Rect rZone;

	int curHeight,curWidth=0;

	public enum AnchorPoint
	{
		Left,
		Right,
		Bottom,
		Top,
		Center
	};
	[Tooltip("Where do you want your zone Anchored?")]
	public AnchorPoint AnchorPosition = new AnchorPoint();

	void Start()
	{
		myInput=GameObject.FindWithTag("Player").GetComponent();
		rZone = new Rect(xCoordinate,yCoordinate,theWidth,theHeight);
		myInput.MouseCursorZones[cursorZoneToUpdate]=rZone;//Change the Default Zone to this one
	}
	void LateUpdate()
	{
		curHeight=Screen.currentResolution.height;
		curWidth=Screen.currentResolution.width;
		if(curHeight != (int)theHeight||curWidth != (int)theWidth)
		{
			UpdateMyResolution();
		}
	}
	//This is a helper GUI so that you can see where your box is being drawn
	void OnGUI () 
	{
		if(AnchorPosition == AnchorPoint.Bottom)
		{
			xCoordinate=(Screen.width-theWidth)/2;
			yCoordinate=Screen.height-theHeight-padding;
		}
		if(AnchorPosition == AnchorPoint.Top)
		{
			xCoordinate=(Screen.width-theWidth)/2;
			yCoordinate=0+padding;
		}
		if(AnchorPosition == AnchorPoint.Left)
		{
			xCoordinate=0+padding;
			yCoordinate=(Screen.height-theHeight)/2;
		}
		if(AnchorPosition == AnchorPoint.Right)
		{
			xCoordinate=(Screen.width-theWidth)-padding;
			yCoordinate=(Screen.height-theHeight)/2;
		}
		if(AnchorPosition == AnchorPoint.Center)
		{
			xCoordinate=(Screen.width-theWidth)/2-padding;
			yCoordinate=(Screen.height-theHeight)/2+padding;
		}
		if(showGUIHelper==true)
		{
			//GUI.Box(new Rect((Screen.width-theWidth)/2,Screen.height-theHeight-5f,theWidth,theHeight+5), "Press Enter to Use Menu");
			GUI.Box(new Rect(xCoordinate,yCoordinate,theWidth,theHeight+5), "Press Enter to Use Menu");
		}
		if(GUI.changed)
		{
			//GUI.Box(new Rect((Screen.width-theWidth)/2,Screen.height-theHeight-5f,theWidth,theHeight+5), "Press Enter to Use Menu");
			GUI.Box(new Rect(xCoordinate,yCoordinate,theWidth,theHeight+5), "Press Enter to Use Menu");
		}
	}
	public void UpdateMyResolution()
	{
		if(AnchorPosition == AnchorPoint.Bottom)
		{
			xCoordinate=(Screen.width-theWidth)/2;
			yCoordinate=Screen.height-theHeight-padding;
		}
		if(AnchorPosition == AnchorPoint.Top)
		{
			xCoordinate=(Screen.width-theWidth)/2;
			yCoordinate=0+padding;
		}
		if(AnchorPosition == AnchorPoint.Left)
		{
			xCoordinate=0+padding;
			yCoordinate=(Screen.height-theHeight)/2;
		}
		if(AnchorPosition == AnchorPoint.Right)
		{
			xCoordinate=(Screen.width-theWidth)-padding;
			yCoordinate=(Screen.height-theHeight)/2;
		}
		if(AnchorPosition == AnchorPoint.Center)
		{
			xCoordinate=(Screen.width-theWidth)/2-padding;
			yCoordinate=(Screen.height-theHeight)/2+padding;
		}
		rZone = new Rect(xCoordinate,yCoordinate,theWidth,theHeight);
		myInput.MouseCursorZones[cursorZoneToUpdate]=rZone;//Change the Default Zone to this one
	}

}

Weapon Switching Code – This is more of a Hack than actually switching weapons. It’s more of a Weapon 3d Model switch so that you can use the same Weapon settings. I just thought it would be a fun concept.

using UnityEngine;
using System.Collections;

public class red_WeaponSwitcher : MonoBehaviour 
{
	[Tooltip("Plug in your weapon reference in the 3rd person view.")]
	public GameObject weapon01Third=null;
	public GameObject projectile01=null;
	public GameObject weapon02Third=null;
	public GameObject projectile02=null;
	public GameObject weapon03Third=null;
	public GameObject projectile03=null;
	public GameObject weapon04Third=null;
	public GameObject projectile04=null;
	public GameObject weapon05Third=null;
	public GameObject projectile05=null;
	public GameObject myGUI;
	
	//Have to find the 1st person weapons as they are instantiated at runtime
	//If we don't have 3rd person weapons, then we need to change this script to look for Strings instead
	GameObject weapon01First;
	GameObject weapon02First;
	GameObject weapon03First;
	GameObject weapon04First;
	GameObject weapon05First;

	//This is our current weapon stuff
	GameObject myWeapon=null;
	vp_FPWeapon myFPWeapon;
	vp_FPWeaponShooter myFPShooter;
	GameObject childWeap;//This is the actual 1st person instantiated weapon
	bool stopChecking=false;

	void Start()
	{
		myFPWeapon=this.GetComponent();
		myFPShooter=this.GetComponent();
		myWeapon=myFPWeapon.gameObject;
	}

	void FixedUpdate()
	{
		if(stopChecking==false)
		{
			childWeap=myWeapon.transform.GetChild(0).gameObject;
			weapon01First=childWeap.transform.FindChild(weapon01Third.name).gameObject;
			weapon02First=childWeap.transform.FindChild(weapon02Third.name).gameObject;
			weapon03First=childWeap.transform.FindChild(weapon03Third.name).gameObject;
			weapon04First=childWeap.transform.FindChild(weapon04Third.name).gameObject;
			weapon05First=childWeap.transform.FindChild(weapon05Third.name).gameObject;

			stopChecking=true;
			myFPShooter.ProjectilePrefab=projectile01;
			updateReferences();
		}
	}

	void OnEnable()
	{
		myGUI.SetActive(true);
	}

	void OnDisable()
	{
		myGUI.SetActive(false);
	}
	public void updateReferences()
	{

		weapon01First.SetActive(true);
		weapon01Third.SetActive(true);
		weapon02First.SetActive(false);
		weapon02Third.SetActive(false);
		weapon03First.SetActive(false);
		weapon03Third.SetActive(false);
		weapon04First.SetActive(false);
		weapon04Third.SetActive(false);
		weapon05First.SetActive(false);
		weapon05Third.SetActive(false);
	}


	public void ChangeWeapon(int myInt)
	{
		switch(myInt)
		{
		case 1:
			//weapon 1
			updateTheWeapons(weapon01First,weapon01Third);
			myFPShooter.ProjectilePrefab=projectile01;
			break;
		case 2:
			//weapon 2
			updateTheWeapons(weapon02First,weapon02Third);
			myFPShooter.ProjectilePrefab=projectile02;
			break;
		case 3:
			//weapon 3
			updateTheWeapons(weapon03First,weapon03Third);
			myFPShooter.ProjectilePrefab=projectile03;
			break;
		case 4:
			//weapon 4
			updateTheWeapons(weapon04First,weapon04Third);
			myFPShooter.ProjectilePrefab=projectile04;
			break;
		case 5:
			//weapon 5
			updateTheWeapons(weapon05First,weapon05Third);
			myFPShooter.ProjectilePrefab=projectile05;
			break;
		}

	}
	void updateTheWeapons(GameObject myObj,GameObject myObj2)
	{
		//Set all to false
		weapon01First.SetActive(false);
		weapon01Third.SetActive(false);
		weapon02First.SetActive(false);
		weapon02Third.SetActive(false);
		weapon03First.SetActive(false);
		weapon03Third.SetActive(false);
		weapon04First.SetActive(false);
		weapon04Third.SetActive(false);
		weapon05First.SetActive(false);
		weapon05Third.SetActive(false);

		//update only this weapon to show

		myObj.SetActive(true);
		myObj2.SetActive(true);
	}
}

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s