Advertisement
lemansky

Untitled

Apr 4th, 2021
735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.41 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.  
  14.     public float jumpSpeed = 10.0f;
  15.     public bool isGrounded = true;
  16.     public bool canDoubleJump = false;
  17.  
  18.     public float rotateSpeed = 5.0f;
  19.     public TextMeshProUGUI healthText;
  20.     public int health = 3;
  21.    
  22.    
  23.     void Start()
  24.     {
  25.         rb = GetComponent<Rigidbody>();
  26.         scoreText = GameObject.Find("Score").GetComponent<TextMeshProUGUI>();
  27.         healthText = GameObject.Find("Health").GetComponent<TextMeshProUGUI>();
  28.         healthText.text = "Health: " + health;
  29.     }
  30.  
  31.     void Update()
  32.     {
  33.         movement = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  34.         movement = transform.TransformDirection(movement);
  35.  
  36.         if (isGrounded)
  37.         {
  38.             if (Input.GetButtonDown("Jump")) {
  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.                     rb.AddForce(Vector3.up * jumpSpeed, ForceMode.Impulse);
  50.                     canDoubleJump = false;
  51.                 }
  52.             }
  53.         }
  54.  
  55.         if (Input.GetMouseButton(1))
  56.         {
  57.             transform.Rotate(Input.GetAxis("Mouse X") * rotateSpeed * Vector3.up);
  58.         }
  59.     }
  60.  
  61.     private void FixedUpdate()
  62.     {
  63.          rb.velocity =  new Vector3(movement.x, 0, movement.z) * speed + new Vector3(0, rb.velocity.y, 0);
  64.     }
  65.  
  66.     public void AddPoints(int awardPoints)
  67.     {
  68.         points += awardPoints;
  69.         scoreText.text = "Score " + points;
  70.     }
  71.  
  72.     public void LoseHealth(int removeHealth)
  73.     {
  74.         health -= removeHealth;
  75.         healthText.text = "Health " + health;
  76.     }
  77.  
  78.     private void OnCollisionEnter(Collision collision)
  79.     {
  80.         if(collision.gameObject.tag == "Ground")
  81.         {
  82.             isGrounded = true;
  83.         }
  84.     }
  85.  
  86.     private void OnCollisionExit(Collision collision)
  87.     {
  88.         if (collision.gameObject.tag == "Ground")
  89.         {
  90.             isGrounded = false;
  91.         }
  92.     }
  93.  
  94.  
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement