lemansky

Untitled

Apr 4th, 2021 (edited)
730
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.81 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.     Rigidbody rb;
  9.    
  10.     public float speed = 10.0f;
  11.  
  12.     Vector3 movement;
  13.     int score = 0;
  14.     public TextMeshProUGUI scoreText;
  15.  
  16.     public float jumpSpeed = 10.0f;
  17.     public bool isGrounded = true;
  18.     public bool canDoubleJump = false;
  19.  
  20.     void Start()
  21.     {
  22.         rb = GetComponent<Rigidbody>();
  23.         scoreText = GameObject.Find("Score").GetComponent<TextMeshProUGUI>();
  24.     }
  25.  
  26.     void Update()
  27.     {
  28.         movement = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  29.  
  30.         if (isGrounded)
  31.         {
  32.             if (Input.GetButtonDown("Jump")) {
  33.                 rb.AddForce(Vector3.up * jumpSpeed, ForceMode.Impulse);
  34.                 canDoubleJump = true;
  35.             }
  36.         }
  37.         else
  38.         {
  39.             if (Input.GetButtonDown("Jump"))
  40.             {
  41.                 if (canDoubleJump)
  42.                 {
  43.                     rb.AddForce(Vector3.up * jumpSpeed, ForceMode.Impulse);
  44.                     canDoubleJump = false;
  45.                 }
  46.             }
  47.         }
  48.     }
  49.  
  50.     private void FixedUpdate()
  51.     {
  52.          rb.velocity =  new Vector3(movement.x, 0, movement.z) * speed + new Vector3(0, rb.velocity.y, 0);
  53.     }
  54.  
  55.     public void AwardPoints(int points)
  56.     {
  57.         score += points;
  58.         scoreText.text = "Score: " + score;
  59.     }
  60.  
  61.     private void OnCollisionEnter(Collision collision)
  62.     {
  63.         if(collision.gameObject.tag == "Ground")
  64.         {
  65.             isGrounded = true;
  66.         }
  67.     }
  68.  
  69.     private void OnCollisionExit(Collision collision)
  70.     {
  71.         if (collision.gameObject.tag == "Ground")
  72.         {
  73.             isGrounded = false;
  74.         }
  75.     }
  76.  
  77.  
  78. }
  79.  
Add Comment
Please, Sign In to add comment