Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using TMPro;
- public class PlayerController : MonoBehaviour
- {
- public float speed = 5.0f;
- Vector3 movement;
- public TextMeshProUGUI scoreText;
- Rigidbody rb;
- public int points = 0;
- public float jumpSpeed = 10.0f;
- public bool canJump = true;
- public bool canDoubleJump = false;
- public float rotateSpeed = 5.0f;
- public TextMeshProUGUI healthText;
- public int health = 3;
- void Start()
- {
- rb = GetComponent<Rigidbody>();
- scoreText = GameObject.Find("Score").GetComponent<TextMeshProUGUI>();
- healthText = GameObject.Find("Health").GetComponent<TextMeshProUGUI>();
- healthText.text = "Health: " + health;
- }
- void Update()
- {
- movement = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
- movement = transform.TransformDirection(movement);
- if (Input.GetButtonDown("Jump") && canJump)
- {
- rb.AddForce(Vector3.up * jumpSpeed, ForceMode.Impulse);
- }
- else if (Input.GetButtonDown("Jump") && canDoubleJump)
- {
- rb.AddForce(Vector3.up * jumpSpeed, ForceMode.Impulse);
- canDoubleJump = false;
- }
- if (Input.GetMouseButton(1))
- {
- transform.Rotate(Input.GetAxis("Mouse X") * rotateSpeed * Vector3.up);
- }
- }
- private void FixedUpdate()
- {
- rb.velocity = new Vector3(movement.x, 0, movement.z) * speed + new Vector3(0, rb.velocity.y, 0);
- }
- public void AddPoints(int awardPoints)
- {
- points += awardPoints;
- scoreText.text = "Score " + points;
- }
- public void LoseHealth(int removeHealth)
- {
- health -= removeHealth;
- healthText.text = "Health " + health;
- }
- private void OnCollisionEnter(Collision collision)
- {
- if (collision.gameObject.tag == "Ground")
- {
- canJump = true;
- }
- }
- private void OnCollisionExit(Collision collision)
- {
- if (collision.gameObject.tag == "Ground")
- {
- canJump = false;
- canDoubleJump = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement