Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.49 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlayerMovement: MonoBehaviour
  5. {
  6.     ///////////////////////// Variables /////////////////////////
  7.    
  8.     //Mouse Vars
  9.     public float mouseSensitivity = 250.0f;
  10.     public float clampAngle = 80.0f;
  11.     private float rotY = 0.0f;
  12.     private float rotX = 0.0f;
  13.  
  14.     //Movement Scheme Vars (Default Values)
  15.     public string[] MoveKey = new string[4] { "w", "s", "a", "d" };
  16.     public string[] Profile1 = new string[4] { "w", "s", "a", "d" };
  17.     public string[] Profile2 = new string[4] { "t", "g", "f", "h" };
  18.     public string[] Profile3 = new string[4] { "Y_Xbox", "A_Xbox", "X_Xbox", "B_Xbox" };
  19.     string XAxis = "Mouse X";
  20.     string YAxis = "Mouse Y";
  21.  
  22.     //Movement Variables
  23.     public float mainSpeed = 5.0f;
  24.     float shiftAdd = 10.0f;
  25.     float maxShift = 15.0f;
  26.     private float totalRun = 1.0f;
  27.  
  28.     //Misc Vars
  29.     string KeyDisplay;
  30.     public int HP = 100;
  31.     public int EnemyHP = 100;
  32.     public float damagetimer = 0;
  33.     int profile = 0;
  34.  
  35.     ///////////////////////// Functions /////////////////////////
  36.    
  37.     //Movement
  38.     private Vector3 GetBaseInput()
  39.     { //returns the basic values, if it's 0 than it's not active.
  40.         Vector3 p_Velocity = new Vector3();
  41.         if (profile == 0 || profile == 1)
  42.         {
  43.             if (Input.GetKey(MoveKey[0]))
  44.             {
  45.                 p_Velocity += new Vector3(0, 0, 1);
  46.             }
  47.             if (Input.GetKey(MoveKey[1]))
  48.             {
  49.                 p_Velocity += new Vector3(0, 0, -1);
  50.             }
  51.             if (Input.GetKey(MoveKey[2]))
  52.             {
  53.                 p_Velocity += new Vector3(-1, 0, 0);
  54.             }
  55.             if (Input.GetKey(MoveKey[3]))
  56.             {
  57.                 p_Velocity += new Vector3(1, 0, 0);
  58.             }
  59.             return p_Velocity;
  60.         }
  61.         else if (profile == 2)
  62.         {
  63.             if (Input.GetButton(MoveKey[0]))
  64.             {
  65.                 p_Velocity += new Vector3(0, 0, 1);
  66.             }
  67.             if (Input.GetButton(MoveKey[1]))
  68.             {
  69.                 p_Velocity += new Vector3(0, 0, -1);
  70.             }
  71.             if (Input.GetButton(MoveKey[2]))
  72.             {
  73.                 p_Velocity += new Vector3(-1, 0, 0);
  74.             }
  75.             if (Input.GetButton(MoveKey[3]))
  76.             {
  77.                 p_Velocity += new Vector3(1, 0, 0);
  78.             }
  79.             return p_Velocity;
  80.         }
  81.         else { return p_Velocity; }
  82.     }
  83.     private void Movement()
  84.     {
  85.         float f = 0.0f;
  86.         Vector3 p = GetBaseInput();
  87.  
  88.         if (Input.GetKey(KeyCode.LeftShift))
  89.         {
  90.             totalRun += Time.deltaTime;
  91.             p = p * totalRun * shiftAdd;
  92.             p.x = Mathf.Clamp(p.x, -maxShift, maxShift);
  93.             p.y = Mathf.Clamp(p.y, -maxShift, maxShift);
  94.             p.z = Mathf.Clamp(p.z, -maxShift, maxShift);
  95.         }
  96.         else
  97.         {
  98.             totalRun = Mathf.Clamp(totalRun * 0.5f, 1f, 1000f);
  99.             p = p * mainSpeed;
  100.         }
  101.  
  102.         p = p * Time.deltaTime;
  103.         Vector3 newPosition = transform.position;
  104.  
  105.         transform.Translate(p);
  106.         newPosition.x = transform.position.x;
  107.         newPosition.z = transform.position.z;
  108.         transform.position = newPosition;
  109.  
  110.     }
  111.     private void Look()
  112.     {
  113.         float mouseX = Input.GetAxis(XAxis);
  114.         float mouseY = -Input.GetAxis(YAxis);
  115.  
  116.         rotY += mouseX * mouseSensitivity * Time.deltaTime;
  117.         rotX += mouseY * mouseSensitivity * Time.deltaTime;
  118.  
  119.         rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);
  120.  
  121.         Quaternion localRotation = Quaternion.Euler(rotX, rotY, 0.0f);
  122.         transform.rotation = localRotation;
  123.     }
  124.  
  125.     //Profiles And Keybinding
  126.     private void ChangeProfile(int profile)
  127.     {
  128.         this.profile = profile;
  129.         switch (profile)
  130.         {
  131.             case 0:
  132.                 MoveKey[0] = Profile1[0];
  133.                 MoveKey[1] = Profile1[1];
  134.                 MoveKey[2] = Profile1[2];
  135.                 MoveKey[3] = Profile1[3];
  136.                 XAxis = "Mouse X";
  137.                 YAxis = "Mouse Y";
  138.                 break;
  139.             case 1:
  140.                 MoveKey[0] = Profile2[0];
  141.                 MoveKey[1] = Profile2[1];
  142.                 MoveKey[2] = Profile2[2];
  143.                 MoveKey[3] = Profile2[3];
  144.                 XAxis = "Horizontal";
  145.                 YAxis = "Vertical";
  146.                 break;
  147.             case 2:
  148.                 MoveKey[0] = Profile3[0];
  149.                 MoveKey[1] = Profile3[1];
  150.                 MoveKey[2] = Profile3[2];
  151.                 MoveKey[3] = Profile3[3];
  152.                 XAxis = "LJoy_Hori";
  153.                 YAxis = "LJoy_Vert";
  154.                 break;
  155.         }
  156.     }
  157.     private void ChangeKey()
  158.     {
  159.         switch (profile)
  160.         {
  161.             case 0:
  162.                 if (MoveKey[0] == "Press Key")
  163.                 {
  164.                     foreach (KeyCode vKey in System.Enum.GetValues(typeof(KeyCode)))
  165.                     {
  166.                         if (Input.GetKeyDown(vKey))
  167.                         {
  168.                             Profile1[0] = vKey.ToString();
  169.                             Profile1[0].ToLower();
  170.                             MoveKey[0] = Profile1[0].ToLower();
  171.                         }
  172.                     }
  173.                 }
  174.                 if (MoveKey[1] == "Press Key")
  175.                 {
  176.                     foreach (KeyCode vKey in System.Enum.GetValues(typeof(KeyCode)))
  177.                     {
  178.                         if (Input.GetKeyDown(vKey))
  179.                         {
  180.                             Profile1[1] = vKey.ToString();
  181.                             Profile1[1].ToLower();
  182.                             MoveKey[1] = Profile1[1].ToLower();
  183.                         }
  184.                     }
  185.                 }
  186.                 if (MoveKey[2] == "Press Key")
  187.                 {
  188.                     foreach (KeyCode vKey in System.Enum.GetValues(typeof(KeyCode)))
  189.                     {
  190.                         if (Input.GetKeyDown(vKey))
  191.                         {
  192.                             Profile1[2] = vKey.ToString();
  193.                             Profile1[2].ToLower();
  194.                             MoveKey[2] = Profile1[2].ToLower();
  195.                         }
  196.                     }
  197.                 }
  198.                 if (MoveKey[3] == "Press Key")
  199.                 {
  200.                     foreach (KeyCode vKey in System.Enum.GetValues(typeof(KeyCode)))
  201.                     {
  202.                         if (Input.GetKeyDown(vKey))
  203.                         {
  204.                             Profile1[3] = vKey.ToString();
  205.                             Profile1[3].ToLower();
  206.                             MoveKey[3] = Profile1[3].ToLower();
  207.                         }
  208.                     }
  209.                 }
  210.  
  211.                 break;
  212.  
  213.             case 1:
  214.                 if (MoveKey[0] == "Press Key")
  215.                 {
  216.                     foreach (KeyCode vKey in System.Enum.GetValues(typeof(KeyCode)))
  217.                     {
  218.                         if (Input.GetKeyDown(vKey))
  219.                         {
  220.                             Profile2[0] = vKey.ToString();
  221.                             Profile2[0].ToLower();
  222.                             MoveKey[0] = Profile2[0].ToLower();
  223.                         }
  224.                     }
  225.                 }
  226.                 if (MoveKey[1] == "Press Key")
  227.                 {
  228.                     foreach (KeyCode vKey in System.Enum.GetValues(typeof(KeyCode)))
  229.                     {
  230.                         if (Input.GetKeyDown(vKey))
  231.                         {
  232.                             Profile2[1] = vKey.ToString();
  233.                             Profile2[1].ToLower();
  234.                             MoveKey[0] = Profile2[1].ToLower();
  235.                         }
  236.                     }
  237.                 }
  238.                 if (MoveKey[2] == "Press Key")
  239.                 {
  240.                     foreach (KeyCode vKey in System.Enum.GetValues(typeof(KeyCode)))
  241.                     {
  242.                         if (Input.GetKeyDown(vKey))
  243.                         {
  244.                             Profile2[2] = vKey.ToString();
  245.                             Profile2[2].ToLower();
  246.                             MoveKey[0] = Profile2[2].ToLower();
  247.                         }
  248.                     }
  249.                 }
  250.                 if (MoveKey[3] == "Press Key")
  251.                 {
  252.                     foreach (KeyCode vKey in System.Enum.GetValues(typeof(KeyCode)))
  253.                     {
  254.                         if (Input.GetKeyDown(vKey))
  255.                         {
  256.                             Profile2[3] = vKey.ToString();
  257.                             Profile2[3].ToLower();
  258.                             MoveKey[0] = Profile2[3].ToLower();
  259.                         }
  260.                     }
  261.                 }
  262.                 break;
  263.         }
  264.     }
  265.  
  266.     //HP and Damage
  267.     public void ChangeHP(int damage)
  268.     {
  269.         HP -= damage;
  270.     }
  271.     public void RestoreHP()
  272.     {
  273.         if(damagetimer == 0 && HP < 100)
  274.         {
  275.             HP += 2;
  276.             damagetimer = 40;
  277.         }
  278.         if(HP > 100) { HP = 100; }
  279.         if(damagetimer > 0) { damagetimer -= .2f; }
  280.         if(damagetimer < .1) { damagetimer = 0; }
  281.     }
  282.  
  283.     //Misc Functions
  284.     private void CheckKey()
  285.     {
  286.         foreach (KeyCode vKey in System.Enum.GetValues(typeof(KeyCode)))
  287.         {
  288.             if (Input.GetKeyDown(vKey))
  289.             {
  290.                 KeyDisplay = vKey.ToString();
  291.                 //Debug.Log(KeyDisplay);
  292.             }
  293.             else if (Input.GetKeyUp(vKey))
  294.             {
  295.                 KeyDisplay = "";
  296.             }
  297.         }
  298.     }
  299.     private void LockCursor()
  300.     {
  301.         if (Input.GetKeyDown("tab") && Cursor.lockState != CursorLockMode.Locked)
  302.         {
  303.             Cursor.lockState = CursorLockMode.Locked;
  304.         }
  305.         else if (Input.GetKeyDown("tab") && Cursor.lockState == CursorLockMode.Locked)
  306.         {
  307.             Cursor.lockState = CursorLockMode.None;
  308.         }
  309.     }
  310.  
  311.     ///////////////////////// Unity Functions /////////////////////////
  312.  
  313.     void Start()
  314.     {
  315.         Cursor.lockState = CursorLockMode.Locked;
  316.         Vector3 rot = transform.localRotation.eulerAngles;
  317.         rotY = rot.y;
  318.         rotX = rot.x;
  319.     }
  320.     void Update()
  321.     {
  322.         //Movement
  323.         if (Cursor.lockState == CursorLockMode.Locked)
  324.         {
  325.             Look();
  326.             Movement();
  327.         }
  328.  
  329.         //Misc Functions
  330.         CheckKey();
  331.         LockCursor();
  332.         RestoreHP();
  333.  
  334.         if (Input.GetKeyDown("o"))
  335.             ChangeHP(-5);
  336.         if (Input.GetKeyDown("p"))
  337.             ChangeHP(5);
  338.     }
  339.     void OnCollisionEnter(Collision col)
  340.     {
  341.         if (col.gameObject.tag == "Enemy")
  342.         {
  343.             HP -= 5;
  344.         }
  345.         HP -= 5;
  346.     }
  347.     void OnGUI()
  348.     {
  349.         float mouseX = Input.GetAxis(XAxis);
  350.         float mouseY = Input.GetAxis(YAxis);
  351.  
  352.         //Input Switching
  353.         if (Cursor.lockState == CursorLockMode.None)
  354.         {
  355.             GUI.Box(new Rect(25, 50, 250, 300), "");
  356.  
  357.             if (GUI.Button(new Rect(40, 65, 200, 25), "Move Forward: " + MoveKey[0].ToUpper()))
  358.             {
  359.                 MoveKey[0] = "Press Key";
  360.             }
  361.             if (GUI.Button(new Rect(40, 100, 200, 25), "Move Back: " + MoveKey[1].ToUpper()))
  362.             {
  363.                 MoveKey[1] = "Press Key";
  364.             }
  365.             if (GUI.Button(new Rect(40, 135, 200, 25), "Move Left: " + MoveKey[2].ToUpper()))
  366.             {
  367.                 MoveKey[2] = "Press Key";
  368.             }
  369.             if (GUI.Button(new Rect(40, 170, 200, 25), "Move Right: " + MoveKey[3].ToUpper()))
  370.             {
  371.                 MoveKey[3] = "Press Key";
  372.             }
  373.             ChangeKey();
  374.  
  375.             //Profile Switching
  376.             if (GUI.Button(new Rect(40, 235, 125, 20), "Profile 1"))
  377.             {
  378.                 ChangeProfile(0);
  379.             }
  380.             if (GUI.Button(new Rect(40, 260, 125, 20), "Profile 2"))
  381.             {
  382.                 ChangeProfile(1);
  383.             }
  384.             if (GUI.Button(new Rect(40, 285, 125, 20), "Profile 3"))
  385.             {
  386.                 ChangeProfile(2);
  387.             }
  388.         }
  389.  
  390.         //Health Bar
  391.         GUI.backgroundColor = Color.red;
  392.         GUI.Button(new Rect(25, 800, HP+50, 20), "HP: " + HP.ToString());
  393.  
  394.         GUI.backgroundColor = Color.blue;
  395.         GUI.Button(new Rect(25, 825, EnemyHP + 50, 20), "HP: " + EnemyHP.ToString());
  396.  
  397.     }
  398.  
  399. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement