Cursor position on screen

Hello there.

I’m making a game that its combat is RPG like and I want to show a pointer on the characters, abilities menu and enemies on the following order:

  1. Choose a character
  2. Choose a ability
  3. Choose a target enemy

I’m currently able to circle through the characters but I’m failing to make them apear on the abilities list and on the enemies.

Here is my code:

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

public class BattleManager : MonoBehaviour
{
	public bool isPlayerTurn;
	public int nEnemies = 2;

	public Texture2D pointer;


	EnemyManager enemyManager;

	enum MenuState
	{
		ChoosingCharacter,
		ChoosingSpell,
		ChoosingTarget
	}

	public Transform [] players;
	Character [] playerCharacters;

	Character currentlySelectedCharacter;

	int currentSelectionIndex = 0;
	float menuInputTimer;

	MenuState currentMenuState;

	Vector2 pointerPosition;


	// Use this for initialization
	void Start ()
	{
		playerCharacters = new Character[players.Length];

		for (int i = 0; i < players.Length; ++i)
		{
			playerCharacters _= players*.GetComponent<Character>();*_

* }*

* enemyManager = FindObjectOfType();*
* }*

* // Update is called once per frame*
* void Update ()*
* {*
* if (menuInputTimer > 0)*
* {*
* menuInputTimer -= Time.deltaTime;*
* }*
* else*
* {*
* if (Input.GetAxisRaw(“Vertical”) > 0)*
* {*
* currentSelectionIndex ++;*
* menuInputTimer = 0.1f;*
* }*
* else if (Input.GetAxisRaw(“Vertical”) < 0)*
* {*
* currentSelectionIndex --; *
* menuInputTimer = 0.1f;*
* }*

* switch(currentMenuState)*
* {*
* case MenuState.ChoosingCharacter:*
* if (currentSelectionIndex >= playerCharacters.Length)*
* {*
* currentSelectionIndex = 0;*
* }*
* else if (currentSelectionIndex < 0)*
* {*
* currentSelectionIndex = playerCharacters.Length - 1;*
* }*

* //Calculate where on screen the current selected player is*
* pointerPosition = Camera.main.WorldToScreenPoint(players[currentSelectionIndex].position);*

* if (Input.GetButtonDown(“Select”))*
* {*
* currentlySelectedCharacter = playerCharacters[currentSelectionIndex];*
* currentMenuState = MenuState.ChoosingSpell;*
* currentSelectionIndex = 0;*
* }*
* break;*

* case MenuState.ChoosingSpell:*
* if (currentSelectionIndex >= currentlySelectedCharacter.spells.Length)*
* {*
* currentSelectionIndex = 0;*
* }*
* else if (currentSelectionIndex < 0)*
* {*
* currentSelectionIndex = currentlySelectedCharacter.spells.Length - 1;*
* }*

* if (Input.GetButtonDown(“Cancel”))*
* {*
* currentMenuState = MenuState.ChoosingCharacter;*
* currentSelectionIndex = 0;*
* }*

* if (Input.GetButtonDown(“Select”))*
* {*
* currentMenuState = MenuState.ChoosingTarget;*
* currentSelectionIndex = 0;*
* }*
* break;*

* case MenuState.ChoosingTarget:*
* if(currentSelectionIndex >= enemyManager.enemies.Length)*
* {*
* currentSelectionIndex = 0;*
* }*
* else if (currentSelectionIndex < 0)*
* {*
* currentSelectionIndex = enemyManager.enemies.Length - 1;*
* }*

* break;*
* }*
* }*
* }*

* void OnGUI()*
* {*
* switch(currentMenuState)*
* {*
* case MenuState.ChoosingCharacter:*
* GUI.DrawTexture(new Rect(pointerPosition.x + 30, Screen.height - pointerPosition.y, pointer.width, pointer.height), pointer);*

* break;*

* case MenuState.ChoosingSpell:*

* int maxAbilityWidth = Screen.width / currentlySelectedCharacter.spells.Length;*

* for (int i = 0 ; i < currentlySelectedCharacter.spells.Length; ++i)*
* {*
_ GUI.Button(new Rect(i * Mathf.Min(maxAbilityWidth, 100) + 700, Screen.height - 100, Mathf.Min(maxAbilityWidth, 100), 50)
,currentlySelectedCharacter.spells*.name);
}*_

* break;*

* case MenuState.ChoosingTarget:*
* GUI.DrawTexture(new Rect(pointerPosition.x + 30, Screen.height - pointerPosition.y, pointer.width, pointer.height), pointer);*

* break;*
* }*
* }*
}

I’m trying to make the code as generic as I can.
Thanks in advance.

And this is how I did it :slight_smile:

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

public class BattleManager : MonoBehaviour
{
	public bool isPlayerTurn;

	public Transform enemies;
	List<Enemy> enemyCharacters = new List<Enemy>();

	int enemyIndex = 0;

	public Texture2D pointer;


	EnemyManager enemyManager;

	enum MenuState
	{
		ChoosingCharacter,
		ChoosingSpell,
		ChoosingTarget
	}

	public Transform [] players;
	Character [] playerCharacters;

	Character currentlySelectedCharacter;

	int currentSelectionIndex = 0;
	float menuInputTimer;

	MenuState currentMenuState;

	Vector2 pointerPosition;




	// Use this for initialization
	void Start ()
	{
		playerCharacters = new Character[players.Length];

		for (int i = 0; i < players.Length; ++i)
		{
			playerCharacters _= players*.GetComponent<Character>();*_

* }*

* enemyManager = FindObjectOfType();*
* }*

* // Update is called once per frame*
* void Update ()*
* {*
* if (menuInputTimer > 0)*
* {*
* menuInputTimer -= Time.deltaTime;*
* }*
* else*
* {*
* if (Input.GetAxisRaw(“Vertical”) > 0)*
* {*
* currentSelectionIndex ++;*
* menuInputTimer = 0.1f;*
* }*
* else if (Input.GetAxisRaw(“Vertical”) < 0)*
* {*
* currentSelectionIndex --; *
* menuInputTimer = 0.1f;*
* }*

* switch(currentMenuState)*
* {*
* case MenuState.ChoosingCharacter:*
* if (currentSelectionIndex >= playerCharacters.Length)*
* {*
* currentSelectionIndex = 0;*
* }*
* else if (currentSelectionIndex < 0)*
* {*
* currentSelectionIndex = playerCharacters.Length - 1;*
* }*

* //Calculate where on screen the current selected player is*
* pointerPosition = Camera.main.WorldToScreenPoint(players[currentSelectionIndex].position);*

* if (Input.GetButtonDown(“Select”))*
* {*
* currentlySelectedCharacter = playerCharacters[currentSelectionIndex];*
* currentMenuState = MenuState.ChoosingSpell;*
* currentSelectionIndex = 0;*
* }*
* break;*

* case MenuState.ChoosingSpell:*
* if (currentSelectionIndex >= currentlySelectedCharacter.spells.Length)*
* {*
* currentSelectionIndex = 0;*
* }*
* else if (currentSelectionIndex < 0)*
* {*
* currentSelectionIndex = currentlySelectedCharacter.spells.Length - 1;*
* }*

* if (Input.GetButtonDown(“Cancel”))*
* {*
* currentMenuState = MenuState.ChoosingCharacter;*
* currentSelectionIndex = 0;*
* }*

* if (Input.GetButtonDown(“Select”))*
* {*
* currentMenuState = MenuState.ChoosingTarget;*
* currentSelectionIndex = 0;*
* }*
* break;*

* case MenuState.ChoosingTarget:*
* if(currentSelectionIndex >= enemyManager.enemies.Count)*
* {*
* currentSelectionIndex = 0;*
* }*
* else if (currentSelectionIndex < 0)*
* {*
* currentSelectionIndex = enemyManager.enemies.Count - 1;*
* } *

* if (Input.GetButtonDown(“Cancel”))*
* {*
* currentMenuState = MenuState.ChoosingSpell;*
* currentSelectionIndex = 0;*
* }*

* break;*
* }*
* }*
* }*

* void OnGUI()*
* {*
* switch(currentMenuState)*
* {*
* case MenuState.ChoosingCharacter:*
* GUI.DrawTexture(new Rect(pointerPosition.x + 30, Screen.height - pointerPosition.y, pointer.width, pointer.height), pointer);*

* break;*

* case MenuState.ChoosingSpell:*

* int maxAbilityWidth = Screen.width / currentlySelectedCharacter.spells.Length;*

* for (int i = 0 ; i < currentlySelectedCharacter.spells.Length; ++i)*
* {*
_ GUI.Button(new Rect(i * Mathf.Min(maxAbilityWidth, 100) + 700, Screen.height - 100, Mathf.Min(maxAbilityWidth, 100), 50)
,currentlySelectedCharacter.spells*.name);
}*_

* //Pointer*
* switch(currentSelectionIndex)*
* {*
* case 0:*
* GUI.DrawTexture(new Rect(currentSelectionIndex + 750, currentSelectionIndex + 600 , pointer.width, pointer.height), pointer);*
* break;*
* case 1:*
* GUI.DrawTexture(new Rect(currentSelectionIndex + 850, currentSelectionIndex + 600 , pointer.width, pointer.height), pointer);*
* break;*
* case 2:*
* GUI.DrawTexture(new Rect(currentSelectionIndex + 950, currentSelectionIndex + 600 , pointer.width, pointer.height), pointer);*
* break;*
* case 3:*
* GUI.DrawTexture(new Rect(currentSelectionIndex + 1050, currentSelectionIndex + 600 , pointer.width, pointer.height), pointer);*
* break;*
* }*

* break;*

* case MenuState.ChoosingTarget:*
* Debug.Log(currentSelectionIndex);*

* switch(currentSelectionIndex)*
* {*
* case 0:*
* GUI.DrawTexture(new Rect(currentSelectionIndex + 355, currentSelectionIndex + 300 , pointer.width, pointer.height), pointer);*
* break;*
* case 1:*
* GUI.DrawTexture(new Rect(currentSelectionIndex + 250, currentSelectionIndex + 470 , pointer.width, pointer.height), pointer);*
* break;*
* }*

* break;*
* }*
* }*
}