- Home /
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:
Choose a character
Choose a ability
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[i] = players[i].GetComponent<Character>();
}
enemyManager = FindObjectOfType<EnemyManager>();
}
// 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[i].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.
Comment
Answer by bemed · Jun 26, 2014 at 09:28 PM
And this is how I did it :)
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[i] = players[i].GetComponent<Character>();
}
enemyManager = FindObjectOfType<EnemyManager>();
}
// 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[i].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;
}
}
}
Your answer
Follow this Question
Related Questions
Cursor Toggle in Menu (UI) 1 Answer
How to throw a rigidbody toward the mouse? 1 Answer
Show/Hide Mouse on Screen Position 1 Answer