Advertisement
EmmyDev

Character Controls

Jan 1st, 2023
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.37 KB | Source Code | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CharControls : MonoBehaviour
  6. {
  7.     //hp managing
  8.     public int health;
  9.     public GameObject hpBar;
  10.     public GameObject blood;
  11.     public GameObject hurtEff;
  12.  
  13.     //setup stuff and RB
  14.     public int ownedByCtrlNum;
  15.     private string XaxisName;
  16.     private string YaxisName;
  17.     private string AbuttonName;
  18.     private string BbuttonName;
  19.     private float x;
  20.     private float y;
  21.     public float moveSpeed = 720f;
  22.     private Rigidbody2D RB;
  23.  
  24.     //is grounded?
  25.     private bool isGrounded;
  26.     public Transform groundCheck;
  27.     public float gcRadius;
  28.     public LayerMask groundMask;
  29.     public bool hasExploded;
  30.     public float jumpForce;
  31.     public bool jumpInProg;
  32.  
  33.     //animations
  34.     public AnimationClip Idle;
  35.     public AnimationClip Walk;
  36.     public AnimationClip Jump;
  37.     public AnimationClip Toss;
  38.     public AnimationClip Reload;
  39.  
  40.     private Animator anim;
  41.     //shooting
  42.     public int snowballCount = 3;
  43.     private float shtTimer;
  44.     private bool isShooting;
  45.     public GameObject snowball;
  46.     public GameObject sandball;
  47.     public GameObject polkaball;
  48.     public Transform SP;
  49.     private GameObject Sbclone;
  50.     private float fireRate;
  51.     //reloading
  52.     private float rldTimer;
  53.     private bool reloading;
  54.  
  55.     //invincible status
  56.     public bool invincible;
  57.     public GameObject invSprt;
  58.  
  59.     //specials related
  60.     public bool saw;
  61.     public bool smg;
  62.     public bool shield;
  63.     private float shieldTimer;
  64.     private float sawTimer;
  65.     private float smgTimer;
  66.     public GameObject smgSprt;
  67.     public GameObject sawSprt;
  68.     private int smgCount;
  69.     //Audio
  70.     public AudioSource audioSource;
  71.     public AudioClip[] clips;
  72.     //secrets
  73.     public bool sawkill;
  74.     public bool smgkill;
  75.     public bool bombkill;
  76.    
  77.  
  78.     private void Awake()
  79.     {
  80.         invincible = true;
  81.         health = 4;
  82.         DontDestroyOnLoad(this.gameObject);
  83.     }
  84.     void Start()
  85.     {
  86.         RB = this.GetComponent<Rigidbody2D>();
  87.         anim = this.GetComponent<Animator>();
  88.        
  89.     }
  90.     void Update()
  91.     {
  92.  
  93.         //NEEDS WORK
  94.        
  95.         if(ownedByCtrlNum == 4 && Input.GetJoystickNames().Length < 4){
  96.             //keyboard exception for player4 if controller isnt connected.
  97.             AbuttonName = "AK";
  98.             BbuttonName = "BK";
  99.             XaxisName = "LRK";
  100.             YaxisName = "UDK";
  101.         }
  102.         else
  103.         {
  104.             //identification
  105.             XaxisName = "LR" + ownedByCtrlNum.ToString();
  106.             YaxisName = "UD" + ownedByCtrlNum.ToString();
  107.             AbuttonName = "A" + ownedByCtrlNum.ToString();
  108.             BbuttonName = "B" + ownedByCtrlNum.ToString();
  109.         }
  110.  
  111.  
  112.  
  113.         //Flip Charcter
  114.         if(x == 1 || x == -1)
  115.         {
  116.             this.gameObject.transform.localScale = new Vector3(x, 1, 1);
  117.            
  118.         }
  119.        
  120.         //Movement x
  121.         if(Input.GetAxis(XaxisName) == 1 || Input.GetAxis(XaxisName) == -1)
  122.         {
  123.             x = Input.GetAxis(XaxisName);
  124.             if(isGrounded == true && isShooting == false)
  125.             {
  126.                 anim.Play(Walk.name.ToString());
  127.             }
  128.            
  129.         }
  130.         else
  131.         {
  132.             x = 0f;
  133.             if (isGrounded == true && isShooting == false && reloading == false)
  134.             {
  135.                 anim.Play(Idle.name.ToString());
  136.             }
  137.         }
  138.         if(isGrounded == false && isShooting == false)
  139.         {
  140.             anim.Play(Jump.name.ToString());
  141.         }
  142.            
  143.        
  144.         //Jump & Hold
  145.         isGrounded = Physics2D.OverlapCircle(groundCheck.position, gcRadius, groundMask);
  146.        
  147.         if (Input.GetButtonDown(BbuttonName) && isGrounded == true)
  148.         {
  149.             RB.velocity = new Vector2(RB.velocity.x, jumpForce);
  150.             jumpInProg = true;
  151.         }
  152.        
  153.        
  154.        
  155.        
  156.  
  157.  
  158.  
  159.         //Shoot
  160.         shtTimer += Time.deltaTime;
  161.         if(Input.GetButtonDown(AbuttonName) && snowballCount > 0 && isShooting == false && reloading == false && smg == false)
  162.         {
  163.             fireRate = 0.3f;
  164.             //print("Reg");
  165.             snowballCount -= 1;
  166.             anim.Play(Toss.name.ToString());
  167.             shtTimer = 0f;
  168.             isShooting = true;
  169.             audioSource.clip = clips[1];
  170.             audioSource.Play();
  171.         }else if(Input.GetButton(AbuttonName) && snowballCount > 0 && isShooting == false && reloading == false && smg == true)
  172.         {
  173.             print("Smg");
  174.             if(smg == true){
  175.                 smgCount += 1;
  176.                
  177.             }
  178.             snowballCount -= 1;
  179.             anim.Play(Toss.name.ToString());
  180.             shtTimer = 0f;
  181.             isShooting = true;
  182.         }
  183.         if(isShooting == true)
  184.         {
  185.             if(shtTimer > fireRate)
  186.             {
  187.                 float bs;
  188.                 if(GameObject.Find("GameManagerFollow").GetComponent<GameManager>().MapName == "Egypt" && smg == false){
  189.                     Sbclone = Instantiate(sandball, SP.transform.position, SP.transform.rotation);
  190.                     //The sandy exception
  191.                 }
  192.                 else if(smg == true){
  193.                     //smg exception
  194.                     Sbclone = Instantiate(polkaball, SP.transform.position, SP.transform.rotation);
  195.                 }
  196.                 else{
  197.                     //regular snowball
  198.                     Sbclone = Instantiate(snowball, SP.transform.position, SP.transform.rotation);
  199.                 }
  200.                 if(this.RB.velocity.x > 0){
  201.                     bs = this.RB.velocity.x;
  202.                     //print(bs);
  203.                 }
  204.                 else
  205.                 {
  206.                     bs = this.RB.velocity.x;
  207.                     bs = -bs;
  208.  
  209.                     //print(bs);
  210.                 }
  211.                 Sbclone.transform.localScale = this.transform.localScale;
  212.                 Sbclone.GetComponent<SnowballLogic>().ballSpeed = bs;
  213.                 Sbclone = null;
  214.                 isShooting = false;
  215.             }
  216.         }
  217.  
  218.         //Reload
  219.         y = Input.GetAxis(YaxisName);
  220.         if(y == 1)
  221.         {
  222.             reloading = true;
  223.         }
  224.         if(reloading == true)
  225.         {
  226.             if(snowballCount < 3 && x < 1 && x > -1 && isGrounded == true)
  227.             {
  228.                 anim.Play(Reload.name.ToString());
  229.                 rldTimer += Time.deltaTime;
  230.                 if (rldTimer > 1.5f)
  231.                 {
  232.                     snowballCount = 3;
  233.                 }
  234.             }
  235.             else
  236.             {
  237.                 reloading = false;
  238.                 rldTimer = 0;
  239.             }
  240.         }
  241.         //Health manager
  242.         hpBar.GetComponent<hpBar>().health = health;
  243.  
  244.         if(health <= 0)
  245.         {
  246.             //secrets & unlockables realted to char death
  247.             if(this.gameObject.transform.name == "PlayableChar(Clone)" && sawkill == true){
  248.                 PlayerPrefs.SetInt("Dead",1);
  249.             }
  250.             if(this.gameObject.transform.name == "PlayableChar 1(Clone)" && smgkill == true){
  251.                 PlayerPrefs.SetInt("Shot",1);
  252.             }
  253.             if(this.gameObject.transform.name == "PlayableChar 3(Clone)" && bombkill == true){
  254.                 PlayerPrefs.SetInt("Burn",1);
  255.             }
  256.  
  257.  
  258.             //THE ACTUAL DEATH
  259.             Instantiate(blood.gameObject, new Vector3(transform.position.x, transform.position.y, transform.position.z - 1f), transform.rotation);
  260.             Destroy(this.gameObject);
  261.         }
  262.  
  263.         //Invincible status
  264.         invSprt.SetActive(invincible);
  265.         //Items or Specials
  266.        
  267.         if(saw == true){
  268.             sawTimer += Time.deltaTime;
  269.             sawSprt.SetActive(true);
  270.             snowballCount = 0;
  271.             if(sawTimer >= 4f){
  272.                 saw = false;
  273.             }
  274.         }
  275.         else
  276.         {
  277.             sawSprt.SetActive(false);
  278.             sawTimer = 0f;
  279.         }
  280.         if(smg == true){
  281.             smgSprt.SetActive(true);
  282.             smgTimer += Time.deltaTime;
  283.             fireRate = 0.1f;
  284.             snowballCount = 3;
  285.             if(smgTimer >= 8f)
  286.             {
  287.                
  288.                 snowballCount = 0;
  289.                 smg = false;
  290.                
  291.             }
  292.             else if(smgCount >= 10)
  293.             {
  294.                 isShooting = false;
  295.                 snowballCount = 0;
  296.                 smg = false;
  297.             }
  298.            
  299.         }
  300.         else
  301.         {
  302.             smgSprt.SetActive(false);
  303.             smgTimer = 0f;
  304.            
  305.             smg = false;
  306.             smgCount = 0;
  307.         }
  308.         if(shield == true){
  309.             invincible = true;
  310.             shieldTimer += Time.deltaTime;
  311.             if(shieldTimer >= 3f){
  312.                 shieldTimer = 0f;
  313.                 invincible = false;
  314.                 shield = false;
  315.             }
  316.         }
  317.        
  318.        
  319.     }
  320.     void FixedUpdate()
  321.     {
  322.         this.GetComponent<Rigidbody2D>().velocity = new Vector2(moveSpeed * x, this.GetComponent<Rigidbody2D>().velocity.y);
  323.         //jump holding
  324.         if(jumpInProg == true && !Input.GetButton(BbuttonName))
  325.         {
  326.             RB.velocity -= new Vector2(0, 2.5f);
  327.         }
  328.         if (RB.velocity.y < 0f)
  329.         {
  330.             jumpInProg = false;
  331.         }
  332.        
  333.     }
  334.    
  335.     public void takeDamage(int dmg){
  336.         Instantiate(hurtEff.gameObject, new Vector3(transform.position.x, transform.position.y, transform.position.z - 1f), transform.rotation);
  337.         sawkill = false;
  338.         health -= dmg;
  339.     }
  340.     public void takeDamageSaw(int dmg){
  341.         sawkill = true;
  342.         health -= dmg;
  343.     }
  344.     public void takeDamageSmg(int dmg){
  345.         smgkill = true;
  346.         health -= dmg;
  347.     }
  348.     public void takeDamageBomb(int dmg){
  349.         Instantiate(hurtEff.gameObject, new Vector3(transform.position.x, transform.position.y, transform.position.z - 1f), transform.rotation);
  350.         bombkill = true;
  351.         health -= dmg;
  352.     }
  353.     public void Heal(){
  354.         if(health < 4){
  355.             audioSource.clip = clips[0];
  356.             audioSource.Play();
  357.             health += 1;
  358.         }
  359.         else{
  360.             audioSource.clip = clips[4];
  361.             audioSource.Play();
  362.         }
  363.     }
  364. }
  365.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement