Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by bemed · Jun 25, 2014 at 10:01 PM · cursorpointer

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[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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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;
         }
     }
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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

Cursor doesn't show up after completing game 1 Answer

Showing and locking the cursor at right time. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges