Advertisement
lemansky

Untitled

Apr 4th, 2021 (edited)
794
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.65 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 canJump = 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 (Input.GetButtonDown("Jump") && canJump)
  31.         {
  32.             rb.AddForce(Vector3.up * jumpSpeed, ForceMode.Impulse);
  33.         }
  34.         else if(Input.GetButtonDown("Jump") && canDoubleJump)
  35.         {
  36.             rb.AddForce(Vector3.up * jumpSpeed, ForceMode.Impulse);
  37.             canDoubleJump = false;
  38.         }
  39.  
  40.     }
  41.  
  42.     private void FixedUpdate()
  43.     {
  44.          rb.velocity =  new Vector3(movement.x, 0, movement.z) * speed + new Vector3(0, rb.velocity.y, 0);
  45.     }
  46.  
  47.     public void AwardPoints(int points)
  48.     {
  49.         score += points;
  50.         scoreText.text = "Score: " + score;
  51.     }
  52.  
  53.     private void OnCollisionEnter(Collision collision)
  54.     {
  55.         if(collision.gameObject.tag == "Ground")
  56.         {
  57.             canJump = true;
  58.         }
  59.     }
  60.  
  61.     private void OnCollisionExit(Collision collision)
  62.     {
  63.         if (collision.gameObject.tag == "Ground")
  64.         {
  65.             canJump = false;
  66.             canDoubleJump = true;
  67.         }
  68.     }
  69.  
  70.  
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement