Advertisement
SvetoslavUzunov

PlayerController

Mar 30th, 2022
694
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.70 KB | None | 0 0
  1. using UnityEngine;
  2. using TMPro;
  3.  
  4. public class PlayerController : MonoBehaviour
  5. {
  6.     public float speed = 10.0f;
  7.     Rigidbody rb;
  8.     [SerializeField]
  9.     private Vector3 movement;
  10.     private int points = 0;
  11.     TextMeshPro scoreText;
  12.     TextMeshPro winText;
  13.     private bool finish = false;
  14.     public GameObject newCollectable;
  15.  
  16.     void Start()
  17.     {
  18.         rb = this.GetComponent<Rigidbody>();
  19.         scoreText = GameObject.Find("Score").GetComponent<TextMeshPro>();
  20.         winText = GameObject.Find("Win").GetComponent<TextMeshPro>();
  21.         winText.gameObject.SetActive(false);
  22.     }
  23.  
  24.     void Update()
  25.     {
  26.         if (!finish)
  27.         {
  28.             movement = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  29.         }
  30.         //transform.Translate(movement * speed * Time.deltaTime);
  31.     }
  32.  
  33.     private void FixedUpdate()
  34.     {
  35.         //rb.AddForce(movement * speed);
  36.         //rb.velocity = movement * speed;
  37.         rb.MovePosition(transform.position + (movement * speed * Time.deltaTime));
  38.     }
  39.  
  40.     private void OnTriggerEnter(Collider other)
  41.     {
  42.         if (other.gameObject.CompareTag("Pickup"))
  43.         {
  44.             other.gameObject.SetActive(false);
  45.             points += other.gameObject.GetComponent<Bonus>().Point;
  46.             scoreText.text = $"Score: {points}";
  47.  
  48.             if (points >= 20)
  49.             {
  50.                 winText.gameObject.SetActive(true);
  51.                 finish = true;
  52.             }
  53.             else if (points >= 6)
  54.             {
  55.                 Instantiate(newCollectable, new Vector3(Random.Range(-4.0f, 4.0f), 1.5f, Random.Range(-4.0f, 4.0f)), transform.rotation);
  56.             }
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement