Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class PlayerMovement: MonoBehaviour
- {
- ///////////////////////// Variables /////////////////////////
- //Mouse Vars
- public float mouseSensitivity = 250.0f;
- public float clampAngle = 80.0f;
- private float rotY = 0.0f;
- private float rotX = 0.0f;
- //Movement Scheme Vars (Default Values)
- public string[] MoveKey = new string[4] { "w", "s", "a", "d" };
- public string[] Profile1 = new string[4] { "w", "s", "a", "d" };
- public string[] Profile2 = new string[4] { "t", "g", "f", "h" };
- public string[] Profile3 = new string[4] { "Y_Xbox", "A_Xbox", "X_Xbox", "B_Xbox" };
- string XAxis = "Mouse X";
- string YAxis = "Mouse Y";
- //Movement Variables
- public float mainSpeed = 5.0f;
- float shiftAdd = 10.0f;
- float maxShift = 15.0f;
- private float totalRun = 1.0f;
- //Misc Vars
- string KeyDisplay;
- public int HP = 100;
- public int EnemyHP = 100;
- public float damagetimer = 0;
- int profile = 0;
- ///////////////////////// Functions /////////////////////////
- //Movement
- private Vector3 GetBaseInput()
- { //returns the basic values, if it's 0 than it's not active.
- Vector3 p_Velocity = new Vector3();
- if (profile == 0 || profile == 1)
- {
- if (Input.GetKey(MoveKey[0]))
- {
- p_Velocity += new Vector3(0, 0, 1);
- }
- if (Input.GetKey(MoveKey[1]))
- {
- p_Velocity += new Vector3(0, 0, -1);
- }
- if (Input.GetKey(MoveKey[2]))
- {
- p_Velocity += new Vector3(-1, 0, 0);
- }
- if (Input.GetKey(MoveKey[3]))
- {
- p_Velocity += new Vector3(1, 0, 0);
- }
- return p_Velocity;
- }
- else if (profile == 2)
- {
- if (Input.GetButton(MoveKey[0]))
- {
- p_Velocity += new Vector3(0, 0, 1);
- }
- if (Input.GetButton(MoveKey[1]))
- {
- p_Velocity += new Vector3(0, 0, -1);
- }
- if (Input.GetButton(MoveKey[2]))
- {
- p_Velocity += new Vector3(-1, 0, 0);
- }
- if (Input.GetButton(MoveKey[3]))
- {
- p_Velocity += new Vector3(1, 0, 0);
- }
- return p_Velocity;
- }
- else { return p_Velocity; }
- }
- private void Movement()
- {
- float f = 0.0f;
- Vector3 p = GetBaseInput();
- if (Input.GetKey(KeyCode.LeftShift))
- {
- totalRun += Time.deltaTime;
- p = p * totalRun * shiftAdd;
- p.x = Mathf.Clamp(p.x, -maxShift, maxShift);
- p.y = Mathf.Clamp(p.y, -maxShift, maxShift);
- p.z = Mathf.Clamp(p.z, -maxShift, maxShift);
- }
- else
- {
- totalRun = Mathf.Clamp(totalRun * 0.5f, 1f, 1000f);
- p = p * mainSpeed;
- }
- p = p * Time.deltaTime;
- Vector3 newPosition = transform.position;
- transform.Translate(p);
- newPosition.x = transform.position.x;
- newPosition.z = transform.position.z;
- transform.position = newPosition;
- }
- private void Look()
- {
- float mouseX = Input.GetAxis(XAxis);
- float mouseY = -Input.GetAxis(YAxis);
- rotY += mouseX * mouseSensitivity * Time.deltaTime;
- rotX += mouseY * mouseSensitivity * Time.deltaTime;
- rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);
- Quaternion localRotation = Quaternion.Euler(rotX, rotY, 0.0f);
- transform.rotation = localRotation;
- }
- //Profiles And Keybinding
- private void ChangeProfile(int profile)
- {
- this.profile = profile;
- switch (profile)
- {
- case 0:
- MoveKey[0] = Profile1[0];
- MoveKey[1] = Profile1[1];
- MoveKey[2] = Profile1[2];
- MoveKey[3] = Profile1[3];
- XAxis = "Mouse X";
- YAxis = "Mouse Y";
- break;
- case 1:
- MoveKey[0] = Profile2[0];
- MoveKey[1] = Profile2[1];
- MoveKey[2] = Profile2[2];
- MoveKey[3] = Profile2[3];
- XAxis = "Horizontal";
- YAxis = "Vertical";
- break;
- case 2:
- MoveKey[0] = Profile3[0];
- MoveKey[1] = Profile3[1];
- MoveKey[2] = Profile3[2];
- MoveKey[3] = Profile3[3];
- XAxis = "LJoy_Hori";
- YAxis = "LJoy_Vert";
- break;
- }
- }
- private void ChangeKey()
- {
- switch (profile)
- {
- case 0:
- if (MoveKey[0] == "Press Key")
- {
- foreach (KeyCode vKey in System.Enum.GetValues(typeof(KeyCode)))
- {
- if (Input.GetKeyDown(vKey))
- {
- Profile1[0] = vKey.ToString();
- Profile1[0].ToLower();
- MoveKey[0] = Profile1[0].ToLower();
- }
- }
- }
- if (MoveKey[1] == "Press Key")
- {
- foreach (KeyCode vKey in System.Enum.GetValues(typeof(KeyCode)))
- {
- if (Input.GetKeyDown(vKey))
- {
- Profile1[1] = vKey.ToString();
- Profile1[1].ToLower();
- MoveKey[1] = Profile1[1].ToLower();
- }
- }
- }
- if (MoveKey[2] == "Press Key")
- {
- foreach (KeyCode vKey in System.Enum.GetValues(typeof(KeyCode)))
- {
- if (Input.GetKeyDown(vKey))
- {
- Profile1[2] = vKey.ToString();
- Profile1[2].ToLower();
- MoveKey[2] = Profile1[2].ToLower();
- }
- }
- }
- if (MoveKey[3] == "Press Key")
- {
- foreach (KeyCode vKey in System.Enum.GetValues(typeof(KeyCode)))
- {
- if (Input.GetKeyDown(vKey))
- {
- Profile1[3] = vKey.ToString();
- Profile1[3].ToLower();
- MoveKey[3] = Profile1[3].ToLower();
- }
- }
- }
- break;
- case 1:
- if (MoveKey[0] == "Press Key")
- {
- foreach (KeyCode vKey in System.Enum.GetValues(typeof(KeyCode)))
- {
- if (Input.GetKeyDown(vKey))
- {
- Profile2[0] = vKey.ToString();
- Profile2[0].ToLower();
- MoveKey[0] = Profile2[0].ToLower();
- }
- }
- }
- if (MoveKey[1] == "Press Key")
- {
- foreach (KeyCode vKey in System.Enum.GetValues(typeof(KeyCode)))
- {
- if (Input.GetKeyDown(vKey))
- {
- Profile2[1] = vKey.ToString();
- Profile2[1].ToLower();
- MoveKey[0] = Profile2[1].ToLower();
- }
- }
- }
- if (MoveKey[2] == "Press Key")
- {
- foreach (KeyCode vKey in System.Enum.GetValues(typeof(KeyCode)))
- {
- if (Input.GetKeyDown(vKey))
- {
- Profile2[2] = vKey.ToString();
- Profile2[2].ToLower();
- MoveKey[0] = Profile2[2].ToLower();
- }
- }
- }
- if (MoveKey[3] == "Press Key")
- {
- foreach (KeyCode vKey in System.Enum.GetValues(typeof(KeyCode)))
- {
- if (Input.GetKeyDown(vKey))
- {
- Profile2[3] = vKey.ToString();
- Profile2[3].ToLower();
- MoveKey[0] = Profile2[3].ToLower();
- }
- }
- }
- break;
- }
- }
- //HP and Damage
- public void ChangeHP(int damage)
- {
- HP -= damage;
- }
- public void RestoreHP()
- {
- if(damagetimer == 0 && HP < 100)
- {
- HP += 2;
- damagetimer = 40;
- }
- if(HP > 100) { HP = 100; }
- if(damagetimer > 0) { damagetimer -= .2f; }
- if(damagetimer < .1) { damagetimer = 0; }
- }
- //Misc Functions
- private void CheckKey()
- {
- foreach (KeyCode vKey in System.Enum.GetValues(typeof(KeyCode)))
- {
- if (Input.GetKeyDown(vKey))
- {
- KeyDisplay = vKey.ToString();
- //Debug.Log(KeyDisplay);
- }
- else if (Input.GetKeyUp(vKey))
- {
- KeyDisplay = "";
- }
- }
- }
- private void LockCursor()
- {
- if (Input.GetKeyDown("tab") && Cursor.lockState != CursorLockMode.Locked)
- {
- Cursor.lockState = CursorLockMode.Locked;
- }
- else if (Input.GetKeyDown("tab") && Cursor.lockState == CursorLockMode.Locked)
- {
- Cursor.lockState = CursorLockMode.None;
- }
- }
- ///////////////////////// Unity Functions /////////////////////////
- void Start()
- {
- Cursor.lockState = CursorLockMode.Locked;
- Vector3 rot = transform.localRotation.eulerAngles;
- rotY = rot.y;
- rotX = rot.x;
- }
- void Update()
- {
- //Movement
- if (Cursor.lockState == CursorLockMode.Locked)
- {
- Look();
- Movement();
- }
- //Misc Functions
- CheckKey();
- LockCursor();
- RestoreHP();
- if (Input.GetKeyDown("o"))
- ChangeHP(-5);
- if (Input.GetKeyDown("p"))
- ChangeHP(5);
- }
- void OnCollisionEnter(Collision col)
- {
- if (col.gameObject.tag == "Enemy")
- {
- HP -= 5;
- }
- HP -= 5;
- }
- void OnGUI()
- {
- float mouseX = Input.GetAxis(XAxis);
- float mouseY = Input.GetAxis(YAxis);
- //Input Switching
- if (Cursor.lockState == CursorLockMode.None)
- {
- GUI.Box(new Rect(25, 50, 250, 300), "");
- if (GUI.Button(new Rect(40, 65, 200, 25), "Move Forward: " + MoveKey[0].ToUpper()))
- {
- MoveKey[0] = "Press Key";
- }
- if (GUI.Button(new Rect(40, 100, 200, 25), "Move Back: " + MoveKey[1].ToUpper()))
- {
- MoveKey[1] = "Press Key";
- }
- if (GUI.Button(new Rect(40, 135, 200, 25), "Move Left: " + MoveKey[2].ToUpper()))
- {
- MoveKey[2] = "Press Key";
- }
- if (GUI.Button(new Rect(40, 170, 200, 25), "Move Right: " + MoveKey[3].ToUpper()))
- {
- MoveKey[3] = "Press Key";
- }
- ChangeKey();
- //Profile Switching
- if (GUI.Button(new Rect(40, 235, 125, 20), "Profile 1"))
- {
- ChangeProfile(0);
- }
- if (GUI.Button(new Rect(40, 260, 125, 20), "Profile 2"))
- {
- ChangeProfile(1);
- }
- if (GUI.Button(new Rect(40, 285, 125, 20), "Profile 3"))
- {
- ChangeProfile(2);
- }
- }
- //Health Bar
- GUI.backgroundColor = Color.red;
- GUI.Button(new Rect(25, 800, HP+50, 20), "HP: " + HP.ToString());
- GUI.backgroundColor = Color.blue;
- GUI.Button(new Rect(25, 825, EnemyHP + 50, 20), "HP: " + EnemyHP.ToString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement