Advertisement
lemansky

Untitled

Apr 11th, 2021 (edited)
1,203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.96 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.     int jumpFactor = 1;
  15.     public bool canJump = true;
  16.     public bool canDoubleJump = false;
  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 (Input.GetButtonDown("Jump") && canJump)
  36.         {
  37.             rb.AddForce(Vector3.up * jumpSpeed, ForceMode.Impulse);
  38.         }
  39.         else if (Input.GetButtonDown("Jump") && canDoubleJump)
  40.         {
  41.             if (rb.velocity.y < -2.5f)
  42.             {
  43.                 jumpFactor = 2;
  44.             }
  45.             else
  46.             {
  47.                 jumpFactor = 1;
  48.             }
  49.             rb.AddForce(Vector3.up * jumpSpeed * jumpFactor, ForceMode.Impulse);
  50.             canDoubleJump = false;
  51.         }
  52.  
  53.         if (Input.GetMouseButton(1))
  54.         {
  55.             transform.Rotate(Input.GetAxis("Mouse X") * rotateSpeed * Vector3.up);
  56.         }
  57.     }
  58.  
  59.     private void FixedUpdate()
  60.     {
  61.         rb.velocity = new Vector3(movement.x, 0, movement.z) * speed + new Vector3(0, rb.velocity.y, 0);
  62.     }
  63.  
  64.     private void LateUpdate()
  65.     {
  66.         if (transform.rotation.y != Camera.main.transform.rotation.y)
  67.         {
  68.             Quaternion angle = Quaternion.AngleAxis(Camera.main.transform.rotation.eulerAngles.y, Vector3.up);
  69.             transform.rotation = angle;
  70.         }
  71.     }
  72.  
  73.     public void AwardPoints(int awardPoints)
  74.     {
  75.         points += awardPoints;
  76.         scoreText.text = "Score: " + points;
  77.     }
  78.  
  79.     public void UpdateHealth(int removeHealth)
  80.     {
  81.         health -= removeHealth;
  82.         healthText.text = "Health: " + health;
  83.         if (IsDead())
  84.         {
  85.             GameObject.Find("RespawnPoint").GetComponent<GameController>().GameOverSequence();
  86.         }
  87.     }
  88.  
  89.     public bool IsDead()
  90.     {
  91.         return health <= 0 ? true : false;
  92.     }
  93.  
  94.     private void OnCollisionEnter(Collision collision)
  95.     {
  96.         if (collision.gameObject.tag == "Ground")
  97.         {
  98.             canJump = true;
  99.         }
  100.     }
  101.  
  102.     private void OnCollisionExit(Collision collision)
  103.     {
  104.         if (collision.gameObject.tag == "Ground")
  105.         {
  106.             canJump = false;
  107.             canDoubleJump = true;
  108.         }
  109.     }
  110.  
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement