Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.38 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Mirror;
  5.  
  6. namespace DM
  7. {
  8.     public class ControlManager : NetworkBehaviour
  9.     {
  10.            
  11.  
  12.  
  13.         [Header("Initialize")]
  14.         public GameObject activeModel;  // defines the current active model.
  15.         public string[] randomAttacks;  // array of normal attacks in string.
  16.  
  17.  
  18.         [Header("Inputs")]
  19.         public float vertical;  // stores vertical input.
  20.         public float horizontal; // stores horizontal input.
  21.         public float moveAmount;    //shows the amount of movement from 0 to 1.
  22.         public Vector3 moveDir;     //stores the moving vector value of main character.
  23.  
  24.         [Header("Stats")]
  25.         public float moveSpeed = 8f;  //speed of running
  26.         public float sprintSpeed = 12f;  //speed of sprinting(double time of running)
  27.         public float rotateSpeed = 3f;   //speed of character's turning around
  28.         public float jumpForce = 600f;  //how high you can jump value.
  29.        
  30.  
  31.         [Header("States")]
  32.         public bool onGround;   //shows you are on ground or not.
  33.         public bool sprint;     //shows you are sprinting or not.
  34.         [HideInInspector]
  35.         public bool jump;       //stores whether you jump or not
  36.         [HideInInspector]
  37.         public bool normalAttack;   //stores whether you do normal attack or not
  38.         [HideInInspector]
  39.         public bool comboAttack;       //stores whether you combo or not
  40.         public bool canMove;    //shows you can move or not
  41.         [HideInInspector]
  42.         public bool roll;       //stores whether you roll or not
  43.  
  44.  
  45.         float fixedDelta;        //stores Time.fixedDeltaTime
  46.         float delta;
  47.         Animator anim;      //for caching Animator component
  48.         [HideInInspector]
  49.         public Rigidbody rigid;     //for caching Rigidbody component
  50.         CameraManager camManager;   //for caching CameraManager script
  51.  
  52.         Joystick joystick;
  53.        
  54.         void Start() // Initiallizing camera, animator, rigidboy
  55.         {                  
  56.             camManager = CameraManager.singleton;
  57.             camManager.Init(this.transform);
  58.             SetupAnimator();
  59.             rigid = GetComponent<Rigidbody>();            
  60.         }
  61.        
  62.         void SetupAnimator()//Setting up Animator component in the hierarchy.
  63.         {
  64.             if (activeModel == null)
  65.             {
  66.                 anim = GetComponentInChildren<Animator>();//Find animator component in the children hierarchy.
  67.                 if (anim == null)
  68.                 {
  69.                     Debug.Log("No model");
  70.                 }
  71.                 else
  72.                 {
  73.                     activeModel = anim.gameObject; //save this gameobject as active model.
  74.                 }
  75.             }
  76.  
  77.             if (anim == null)
  78.                 anim = activeModel.GetComponent<Animator>();            
  79.         }
  80.        
  81.         void FixedUpdate () //Since this is physics based controller, you have to use FixedUpdate.
  82.         {
  83.             fixedDelta = Time.fixedDeltaTime;    //storing the last frame updated time.            
  84.  
  85.             FixedTick(fixedDelta);   //update anything related to character moving.
  86.             camManager.FixedTick(fixedDelta);     //update anything related to camera moving.      
  87.         }
  88.        
  89.         void Update()
  90.         {
  91.  
  92.             GetInput();     //getting control input from keyboard or joypad
  93.             UpdateStates();   //Updating anything related to character's actions.        
  94.         }
  95.  
  96.  
  97.  
  98.         void GetInput() //getting various inputs from keyboard or joypad.
  99.         {
  100.             if (isLocalPlayer)
  101.             {
  102.  
  103.                 vertical = Input.GetAxis("Vertical");
  104.                 horizontal = Input.GetAxis("Horizontal");
  105.  
  106.                 vertical = Input.touchCount;
  107.                  horizontal = Input.GetAxis("Mouse X");
  108.  
  109.                  
  110.  
  111.                 sprint = Input.GetKeyDown("1");     //for getting sprint input.
  112.                 jump = Input.GetButtonDown("Jump");      //for getting jump input.
  113.                 normalAttack = Input.GetButtonDown("Fire1"); //for getting normal attack input.
  114.                 comboAttack = Input.GetButtonDown("Fire2");    //for getting combo attack input.
  115.                 roll = Input.GetButtonDown("Fire3");     //for getting roll input.
  116.  
  117.             }            
  118.         }
  119.        
  120.        
  121.         void UpdateStates() //updates character's various actions.
  122.         {
  123.             canMove = anim.GetBool("canMove");   //getting bool value from Animator's parameter named "canMove".          
  124.  
  125.             if (jump)   //I clicked jump, left mouse button or B key in the joypad.
  126.             {
  127.                 if (onGround && canMove) //do jump only when you are on ground and you can move.
  128.                 {
  129.                     anim.CrossFade("falling", 0.1f); //play "falling" animation in 0.1 second as cross fade method.
  130.                     rigid.AddForce(0, jumpForce, 0);  //Adding force to Y axis for jumping up.                  
  131.                 }            
  132.             }  
  133.  
  134.             if(comboAttack)     //I clicked for combo attack. right mouse button or X key in the joypad.
  135.             {
  136.                 if(onGround)    //only when you are on ground.
  137.                 {
  138.                     anim.SetTrigger("combo");   //Set trigger named "combo" on
  139.                    
  140.                 }
  141.             }
  142.  
  143.             if(roll && onGround)    //I clicked for roll. middle mouse button or Y key in the joypad.
  144.             {                
  145.                 anim.SetTrigger("roll");    //Set trigger named "roll" on
  146.             }            
  147.            
  148.             float targetSpeed = moveSpeed;  //set run speed as target speed.
  149.              
  150.             if (sprint)
  151.             {
  152.                 targetSpeed = sprintSpeed;    //set sprint speed as target speed.            
  153.             }
  154.  
  155.             //mixing camera rotation value to the character moving value.
  156.             Vector3 v = vertical * camManager.transform.forward;
  157.             Vector3 h = horizontal * camManager.transform.right;            
  158.  
  159.             //multiplying target speed and move amount.
  160.             moveDir = ((v + h).normalized) * (targetSpeed * moveAmount);            
  161.  
  162.             //This is for isolating y velocity from the character control.
  163.             moveDir.y = rigid.velocity.y;            
  164.            
  165.             //This is for limiting values from 0 to 1.
  166.             float m = Mathf.Abs(horizontal) + Mathf.Abs(vertical);
  167.             moveAmount = Mathf.Clamp01(m);
  168.            
  169.             if (normalAttack && canMove) // I clicked for normal attack when I can move around.
  170.             {
  171.                 string targetAnim;
  172.  
  173.                 //chosing random attack from array.
  174.                 int r = Random.Range(0, randomAttacks.Length);
  175.                 targetAnim = randomAttacks[r];
  176.  
  177.                 anim.CrossFade(targetAnim, 0.1f); //play the target animation in 0.1 second.                
  178.  
  179.                 if (!onGround)
  180.                 {
  181.                     anim.CrossFade("JumpAttack", 0.1f); // When you are air born, you do this jump attack.
  182.                 }
  183.  
  184.                 normalAttack = false;
  185.             }
  186.                        
  187.         }
  188.  
  189.         void FixedTick(float d)
  190.         {
  191.             float pDelta = d;            
  192.  
  193.             if (onGround)
  194.             {
  195.                 if(canMove)
  196.                 {
  197.                     rigid.velocity = moveDir;  //This controls the character movement.                  
  198.                 }                
  199.             }            
  200.  
  201.             //This can control character's rotation.
  202.             if (canMove)
  203.             {
  204.                 Vector3 targetDir = moveDir;
  205.                 targetDir.y = 0;
  206.                 if (targetDir == Vector3.zero)
  207.                     targetDir = transform.forward;
  208.  
  209.                 Quaternion tr = Quaternion.LookRotation(targetDir);
  210.                 Quaternion targetRotation = Quaternion.Slerp(transform.rotation, tr, pDelta * moveAmount * rotateSpeed);
  211.                 transform.rotation = targetRotation;
  212.             }  
  213.  
  214.             HandleMovementAnimations(); //update character's animations.
  215.         }
  216.  
  217.         void HandleMovementAnimations()
  218.         {
  219.            
  220.             anim.SetBool("sprint", sprint);   //syncing sprint bool value to animator's "Sprint" value.          
  221.             if(moveAmount == 0)
  222.             {
  223.                 anim.SetBool("sprint", false);
  224.             }            
  225.            
  226.             anim.SetFloat("vertical", moveAmount, 0.2f, fixedDelta); //syncing moveAmount value to animator's "vertical" value.
  227.         }
  228.  
  229.         //These mecanic detects whether you are on ground or not.
  230.        
  231.         private void OnTriggerStay(Collider collision)
  232.         {
  233.             if (collision.gameObject.tag == "Ground")
  234.             {
  235.                 onGround = true;                
  236.                 anim.SetBool("onGround", true);                
  237.             }
  238.         }
  239.  
  240.         //These mecanic detects whether you are on ground or not.
  241.         private void OnTriggerExit(Collider collision)
  242.         {
  243.             if (collision.gameObject.tag == "Ground")
  244.             {
  245.                 onGround = false;
  246.                 anim.SetBool("onGround", false);                
  247.             }
  248.         }        
  249.  
  250.     }
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement