Advertisement
Guest User

Untitled

a guest
Dec 27th, 2022
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | Source Code | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CursorBehaviour : MonoBehaviour
  6. {
  7. private string XaxisName;
  8. private string AbuttonName;
  9. public int playerNum;
  10. public Transform[] positions; //player sprite array
  11. public int stepPhase; //Wich character is selected?
  12. private bool resetAxis; //1-time push for Input.Getxis command
  13. public int layerCounter; //used to switch between the curors if overlapping
  14. private float timer;
  15. public GameObject[] playableChars; //instantiatable chars
  16. private GameObject[] players;
  17. private float horizontalInput; //this float acts as a way of changing the inpt axis maunally as u cannot do Input.SetAxis(name, value)
  18.  
  19. void Update()
  20. {
  21. //keyboard support
  22. if(playerNum == 4 && Input.GetJoystickNames().Length < 4){
  23. //keyboard exception for player4 if controller isnt connected.
  24. AbuttonName = "AK";
  25. //Debug.Log(playerNum);
  26. XaxisName = "LRK";
  27.  
  28. }
  29. else
  30. {
  31. //identification of controller
  32.  
  33. XaxisName = "LR" + playerNum.ToString();
  34. AbuttonName = "A" + playerNum.ToString();
  35. }
  36.  
  37. //layering
  38. this.GetComponent<SpriteRenderer>().sortingOrder = layerCounter;
  39. timer += Time.deltaTime;
  40. if(timer >= 1f)
  41. {
  42. if(layerCounter > 5)
  43. {
  44. layerCounter = 2;
  45. }
  46. else
  47. {
  48. layerCounter += 1;
  49. }
  50.  
  51. timer = 0f;
  52. }
  53.  
  54. //--identification & phases--
  55.  
  56.  
  57.  
  58.  
  59. //All x-axis-inputs gets turned into this float:
  60.  
  61. horizontalInput = Input.GetAxis(XaxisName);
  62.  
  63.  
  64.  
  65. //selection
  66. if (Input.GetButtonDown(AbuttonName) && positions[stepPhase].gameObject.GetComponent<CharacterSelection>().locked == false)
  67. {
  68. GameObject currentChar = Instantiate(playableChars[stepPhase]);
  69. currentChar.GetComponent<CharControls>().ownedByCtrlNum = playerNum;
  70. this.gameObject.SetActive(false);
  71. }
  72. //winner keeps char
  73. players = GameObject.FindGameObjectsWithTag("Player");
  74. for(int i = 0; i < players.Length; i++){
  75. if(players[i].GetComponent<CharControls>().ownedByCtrlNum == playerNum){
  76. this.gameObject.SetActive(false);
  77. }
  78. }
  79.  
  80.  
  81. //move right
  82. if(horizontalInput == 1 && resetAxis == true)
  83. {
  84. if(stepPhase == 6)
  85. {
  86. stepPhase = 0;
  87. }
  88. else
  89. {
  90. stepPhase += 1;
  91. }
  92. resetAxis = false;
  93. }
  94. //move left
  95. else if(horizontalInput == -1 && resetAxis == true)
  96. {
  97. if (stepPhase == 0)
  98. {
  99. stepPhase = 6;
  100. }
  101. else
  102. {
  103. stepPhase -= 1;
  104. }
  105. resetAxis = false;
  106. }
  107. //part of the 1-time push for Input.Getxis command
  108. if (horizontalInput < 1 && horizontalInput > -1)
  109. {
  110. resetAxis = true;
  111. }
  112.  
  113. //moving the cursor
  114. this.transform.position = positions[stepPhase].position;
  115.  
  116. }
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement