Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class PlayerController : MonoBehaviour
- {
- public SCR_Fireball Fireball;
- //Access the Players Rigidbody
- public Rigidbody playerRigidbody;
- //Hitpoints and Manapoints
- public float p_MaxHitpoints = 100;
- public float p_CurrentHitpoints;
- public Text HealthText;
- public float p_MaxManapoints = 100;
- public float p_CurrentManapoints;
- public Text ManaText;
- public bool TakingDamage = false;
- //Set players Jump Heigh
- public float jumpForce = 100;
- //Set the players movement speed
- public float movementSpeed = 5;
- //Check if the player is touching the ground
- private bool grounded = true;
- //Is scepter equiped
- public bool SceptorEquip = true;
- public GameObject Scepter;
- //Is crossbow Equiped
- public bool CrossbowEquip = false;
- public GameObject Crossbow;
- //Damage Over Time Coroutine
- private IEnumerator DamageOverTime()
- {
- while (TakingDamage == true)
- {
- p_CurrentHitpoints -= 5;
- yield return new WaitForSeconds(2.0f);
- TakingDamage = false;
- }
- }
- // Use this for initialization
- void Start ()
- {
- //Set hitpoints and Manapoints
- p_CurrentHitpoints = p_MaxHitpoints;
- p_CurrentManapoints = p_MaxManapoints;
- //Disable Cursor in-game
- Cursor.lockState = CursorLockMode.Locked;
- Scepter.gameObject.SetActive(true);
- Crossbow.gameObject.SetActive(false);
- }
- // Update is called once per frame
- void Update ()
- {
- //Enable Cursor on button press
- if (Input.GetKeyDown("escape"))
- Cursor.lockState = CursorLockMode.None;
- //Hit points and Mana points
- HealthText.text = "" + p_CurrentHitpoints;
- ManaText.text = "" + p_CurrentManapoints;
- //Movement
- float horizontalAxis = Input.GetAxis("Horizontal");
- float verticalAxis = Input.GetAxis("Vertical");
- Vector3 playerRight = transform.right;
- // Players local forward axis
- Vector3 playerForward = transform.forward;
- // Setting velocity to the local axis
- Vector3 xVelocity = transform.right * horizontalAxis;
- // Change value for forward and back movement.
- Vector3 zVelocity = transform.forward * verticalAxis;
- Vector3 currentTotalVelocity = xVelocity + zVelocity;
- // Normalized vectors reduce lenght to 1 to prevent a faster speed moving diagonally
- currentTotalVelocity = currentTotalVelocity.normalized * movementSpeed;
- currentTotalVelocity.y = playerRigidbody.velocity.y;
- playerRigidbody.velocity = currentTotalVelocity;
- //Get input for Mouse X and Y
- float mouseX = Input.GetAxis("Mouse X");
- float mouseY = Input.GetAxis("Mouse Y");
- // Change Local Euler Angles on key press
- Vector3 currentRotation = transform.localEulerAngles;
- // Rotate from MouseX input
- if (Input.GetAxis("Mouse X") < 0)
- {
- currentRotation.y -= 3;
- }
- if (Input.GetAxis("Mouse X") > 0)
- {
- currentRotation.y += 3;
- }
- transform.localEulerAngles = currentRotation;
- //Mouse Y can rotate all the way around.
- /*
- if (Input.GetAxis("Mouse Y") < 0)
- {
- currentRotation.x += 3;
- }
- if (Input.GetAxis("Mouse Y") > 0)
- {
- currentRotation.x -= 3;
- }
- transform.localEulerAngles = currentRotation;
- */
- //Jump
- if (Input.GetKeyDown("space"))
- {
- if (grounded)
- {
- playerRigidbody.AddForce(0, jumpForce, jumpForce);
- grounded = false;
- }
- }
- //Weapon Swap for Scepter
- if(Input.GetKeyDown("1"))
- {
- Scepter.gameObject.SetActive(true);
- SceptorEquip = true;
- Crossbow.gameObject.SetActive(false);
- CrossbowEquip = false;
- }
- //Weapon Swap for Crossbow
- if (Input.GetKeyDown("2"))
- {
- Scepter.gameObject.SetActive(false);
- SceptorEquip = false;
- Crossbow.gameObject.SetActive(true);
- CrossbowEquip = true;
- }
- //Death
- if (p_CurrentHitpoints <= 0)
- {
- Destroy(gameObject);
- }
- }
- private void OnCollisionEnter(Collision collision)
- {
- if (collision.transform.tag == "Ground")
- {
- TakingDamage = false;
- grounded = true;
- StopCoroutine(DamageOverTime());
- }
- if (collision.transform.tag == "Enemy")
- {
- TakingDamage = true;
- StartCoroutine(DamageOverTime());
- }
- if (collision.transform.tag == "Acid")
- {
- TakingDamage = true;
- StartCoroutine(DamageOverTime());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement