Advertisement
lemansky

Untitled

Apr 4th, 2021 (edited)
838
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.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.  
  14.     public float jumpSpeed = 10.0f;
  15.     public bool canJump = 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 (Input.GetButtonDown("Jump") && canJump)
  37.         {
  38.             rb.AddForce(Vector3.up * jumpSpeed, ForceMode.Impulse);
  39.         }
  40.         else if (Input.GetButtonDown("Jump") && canDoubleJump)
  41.         {
  42.             rb.AddForce(Vector3.up * jumpSpeed, ForceMode.Impulse);
  43.             canDoubleJump = false;
  44.         }
  45.  
  46.         if (Input.GetMouseButton(1))
  47.         {
  48.             transform.Rotate(Input.GetAxis("Mouse X") * rotateSpeed * Vector3.up);
  49.         }
  50.     }
  51.  
  52.     private void FixedUpdate()
  53.     {
  54.         rb.velocity = new Vector3(movement.x, 0, movement.z) * speed + new Vector3(0, rb.velocity.y, 0);
  55.     }
  56.  
  57.     public void AddPoints(int awardPoints)
  58.     {
  59.         points += awardPoints;
  60.         scoreText.text = "Score " + points;
  61.     }
  62.  
  63.     public void LoseHealth(int removeHealth)
  64.     {
  65.         health -= removeHealth;
  66.         healthText.text = "Health " + health;
  67.     }
  68.  
  69.     private void OnCollisionEnter(Collision collision)
  70.     {
  71.         if (collision.gameObject.tag == "Ground")
  72.         {
  73.             canJump = true;
  74.         }
  75.     }
  76.  
  77.     private void OnCollisionExit(Collision collision)
  78.     {
  79.         if (collision.gameObject.tag == "Ground")
  80.         {
  81.             canJump = false;
  82.             canDoubleJump = true;
  83.         }
  84.     }
  85.  
  86.  
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement