Advertisement
lemansky

Untitled

Apr 11th, 2021
1,153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.23 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5.  
  6. public class PlayerController : MonoBehaviour
  7. {
  8.     public float speed = 5.0f;
  9.     Vector3 movement;
  10.     public TextMeshProUGUI scoreText;
  11.     Rigidbody rb;
  12.     public int points = 0;
  13.     public float jumpSpeed = 5.0f;
  14.  
  15.     public bool isGrounded = true;
  16.     public bool canDoubleJump = true;
  17.  
  18.     public float rotateSpeed = 5.0f;
  19.     public int health = 3;
  20.     public TextMeshProUGUI healthText;
  21.  
  22.     void Start()
  23.     {
  24.         rb = GetComponent<Rigidbody>();
  25.         scoreText = GameObject.Find("Score").GetComponent<TextMeshProUGUI>();
  26.         healthText = GameObject.Find("Health").GetComponent<TextMeshProUGUI>();
  27.         healthText.text = "Health:" + health;
  28.     }
  29.  
  30.     void Update()
  31.     {
  32.         movement = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  33.         movement = transform.TransformDirection(movement);
  34.  
  35.         if(isGrounded)
  36.         {
  37.             if (Input.GetButtonDown("Jump"))
  38.             {
  39.                 rb.AddForce(Vector3.up * jumpSpeed, ForceMode.Impulse);
  40.                 canDoubleJump = true;
  41.             }
  42.         }
  43.         else
  44.         {
  45.             if (Input.GetButtonDown("Jump"))
  46.             {
  47.                 if (canDoubleJump)
  48.                 {
  49.                    
  50.                     if(rb.velocity.y < -2.5f)
  51.                     {
  52.                         rb.AddForce(Vector3.up * jumpSpeed * 2, ForceMode.Impulse);
  53.                         canDoubleJump = false;
  54.                     }
  55.                     else
  56.                     {
  57.                         rb.AddForce(Vector3.up * jumpSpeed, ForceMode.Impulse);
  58.                         canDoubleJump = false;
  59.                     }
  60.                 }
  61.             }
  62.         }
  63.  
  64.         if (Input.GetMouseButton(1))
  65.         {
  66.             transform.Rotate(Input.GetAxis("Mouse X") * rotateSpeed * Vector3.up);
  67.         }
  68.     }
  69.  
  70.     private void FixedUpdate()
  71.     {
  72.         rb.velocity = new Vector3(movement.x, 0, movement.z) * speed + new Vector3(0, rb.velocity.y, 0);
  73.     }
  74.  
  75.     private void LateUpdate()
  76.     {
  77.         if (transform.rotation.y != Camera.main.transform.rotation.y)
  78.         {
  79.             Quaternion angle = Quaternion.AngleAxis(Camera.main.transform.rotation.eulerAngles.y, Vector3.up);
  80.             transform.rotation = angle;
  81.         }
  82.     }
  83.  
  84.     public void AddPoints(int awardPoints)
  85.     {
  86.         points += awardPoints;
  87.         scoreText.text = "Score: " + points;
  88.     }
  89.  
  90.     public void LoseHealth(int removeHealth)
  91.     {
  92.         health -= removeHealth;
  93.         healthText.text = "Health: " + health;
  94.         if (IsDead())
  95.         {
  96.             GameObject.Find("RespawnPoint").GetComponent<GameController>().GameOverSequence();
  97.         }
  98.     }
  99.  
  100.     public bool IsDead()
  101.     {
  102.         return health <= 0 ? true : false;
  103.     }
  104.  
  105.     private void OnCollisionEnter(Collision collision)
  106.     {
  107.         if(collision.gameObject.tag == "Ground")
  108.         {
  109.             isGrounded = true;
  110.         }
  111.     }
  112.  
  113.     private void OnCollisionExit(Collision collision)
  114.     {
  115.         if(collision.gameObject.tag == "Ground")
  116.         {
  117.             isGrounded = false;
  118.         }
  119.     }
  120.  
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement