Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class CursorBehaviour : MonoBehaviour
- {
- private string XaxisName;
- private string AbuttonName;
- public int playerNum;
- public Transform[] positions; //player sprite array
- public int stepPhase; //Wich character is selected?
- private bool resetAxis; //1-time push for Input.Getxis command
- public int layerCounter; //used to switch between the curors if overlapping
- private float timer;
- public GameObject[] playableChars; //instantiatable chars
- private GameObject[] players;
- private float horizontalInput; //this float acts as a way of changing the inpt axis maunally as u cannot do Input.SetAxis(name, value)
- void Update()
- {
- //keyboard support
- if(playerNum == 4 && Input.GetJoystickNames().Length < 4){
- //keyboard exception for player4 if controller isnt connected.
- AbuttonName = "AK";
- //Debug.Log(playerNum);
- XaxisName = "LRK";
- }
- else
- {
- //identification of controller
- XaxisName = "LR" + playerNum.ToString();
- AbuttonName = "A" + playerNum.ToString();
- }
- //layering
- this.GetComponent<SpriteRenderer>().sortingOrder = layerCounter;
- timer += Time.deltaTime;
- if(timer >= 1f)
- {
- if(layerCounter > 5)
- {
- layerCounter = 2;
- }
- else
- {
- layerCounter += 1;
- }
- timer = 0f;
- }
- //--identification & phases--
- //All x-axis-inputs gets turned into this float:
- horizontalInput = Input.GetAxis(XaxisName);
- //selection
- if (Input.GetButtonDown(AbuttonName) && positions[stepPhase].gameObject.GetComponent<CharacterSelection>().locked == false)
- {
- GameObject currentChar = Instantiate(playableChars[stepPhase]);
- currentChar.GetComponent<CharControls>().ownedByCtrlNum = playerNum;
- this.gameObject.SetActive(false);
- }
- //winner keeps char
- players = GameObject.FindGameObjectsWithTag("Player");
- for(int i = 0; i < players.Length; i++){
- if(players[i].GetComponent<CharControls>().ownedByCtrlNum == playerNum){
- this.gameObject.SetActive(false);
- }
- }
- //move right
- if(horizontalInput == 1 && resetAxis == true)
- {
- if(stepPhase == 6)
- {
- stepPhase = 0;
- }
- else
- {
- stepPhase += 1;
- }
- resetAxis = false;
- }
- //move left
- else if(horizontalInput == -1 && resetAxis == true)
- {
- if (stepPhase == 0)
- {
- stepPhase = 6;
- }
- else
- {
- stepPhase -= 1;
- }
- resetAxis = false;
- }
- //part of the 1-time push for Input.Getxis command
- if (horizontalInput < 1 && horizontalInput > -1)
- {
- resetAxis = true;
- }
- //moving the cursor
- this.transform.position = positions[stepPhase].position;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement