Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.96 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5.  
  6. public class PlayerController : MonoBehaviour
  7. {
  8.     public AudioSource coinAudioSource;
  9.     public float walkSpeed = 8f;
  10.     public float jumpSpeed = 7f;
  11.  
  12.     // access the HUD
  13.     public HudManager hud;
  14.  
  15.     //to keep our rigid body
  16.     Rigidbody rb;
  17.  
  18.     //to keep the collider object
  19.     Collider coll;
  20.  
  21.     //flag to keep track of whether a jump started
  22.     bool pressedJump = false;
  23.  
  24.     // Use this for initialization
  25.     void Start()
  26.     {
  27.         //get the rigid body component for later use
  28.         rb = GetComponent<Rigidbody>();
  29.  
  30.         //get the player collider
  31.         coll = GetComponent<Collider>();
  32.  
  33.         //refresh the HUD
  34.         hud.Refresh();
  35.     }
  36.  
  37.     // Update is called once per frame
  38.     void Update()
  39.     {
  40.         // Handle player walking
  41.         WalkHandler();
  42.  
  43.         //Handle player jumping
  44.         JumpHandler();
  45.     }
  46.  
  47.     // Make the player walk according to user input
  48.     void WalkHandler()
  49.     {
  50.         // Set x and z velocities to zero
  51.         rb.velocity = new Vector3(0, rb.velocity.y, 0);
  52.  
  53.         // Distance ( speed = distance / time --> distance = speed * time)
  54.         float distance = walkSpeed * Time.deltaTime;
  55.  
  56.         // Input on x ("Horizontal")
  57.         float hAxis = Input.GetAxis("Horizontal");
  58.  
  59.         // Input on z ("Vertical")
  60.         float vAxis = Input.GetAxis("Vertical");
  61.  
  62.         // Movement vector
  63.         Vector3 movement = new Vector3(hAxis * distance, 0f, vAxis * distance);
  64.  
  65.         // Current position
  66.         Vector3 currPosition = transform.position;
  67.  
  68.         // New position
  69.         Vector3 newPosition = currPosition + movement;
  70.  
  71.         // Move the rigid body
  72.         rb.MovePosition(newPosition);
  73.     }
  74.  
  75.     // Check whether the player can jump and make it jump
  76.     void JumpHandler()
  77.     {
  78.         // Jump axis
  79.         float jAxis = Input.GetAxis("Jump");
  80.  
  81.         // Is grounded
  82.         bool isGrounded = CheckGrounded();
  83.  
  84.         // Check if the player is pressing the jump key
  85.         if (jAxis > 0f)
  86.         {
  87.             // Make sure we've not already jumped on this key press
  88.             if (!pressedJump && isGrounded)
  89.             {
  90.                 // We are jumping on the current key press
  91.                 pressedJump = true;
  92.  
  93.                 // Jumping vector
  94.                 Vector3 jumpVector = new Vector3(0f, jumpSpeed, 0f);
  95.  
  96.                 // Make the player jump by adding velocity
  97.                 rb.velocity = rb.velocity + jumpVector;
  98.             }
  99.         }
  100.         else
  101.         {
  102.             // Update flag so it can jump again if we press the jump key
  103.             pressedJump = false;
  104.         }
  105.     }
  106.  
  107.     // Check if the object is grounded
  108.     bool CheckGrounded()
  109.     {
  110.         // Object size in x
  111.         float sizeX = coll.bounds.size.x;
  112.         float sizeZ = coll.bounds.size.z;
  113.         float sizeY = coll.bounds.size.y;
  114.  
  115.         // Position of the 4 bottom corners of the game object
  116.         // We add 0.01 in Y so that there is some distance between the point and the floor
  117.         Vector3 corner1 = transform.position + new Vector3(sizeX / 2, -sizeY / 2 + 0.01f, sizeZ / 2);
  118.         Vector3 corner2 = transform.position + new Vector3(-sizeX / 2, -sizeY / 2 + 0.01f, sizeZ / 2);
  119.         Vector3 corner3 = transform.position + new Vector3(sizeX / 2, -sizeY / 2 + 0.01f, -sizeZ / 2);
  120.         Vector3 corner4 = transform.position + new Vector3(-sizeX / 2, -sizeY / 2 + 0.01f, -sizeZ / 2);
  121.  
  122.         // Send a short ray down the cube on all 4 corners to detect ground
  123.         bool grounded1 = Physics.Raycast(corner1, new Vector3(0, -1, 0), 0.01f);
  124.         bool grounded2 = Physics.Raycast(corner2, new Vector3(0, -1, 0), 0.01f);
  125.         bool grounded3 = Physics.Raycast(corner3, new Vector3(0, -1, 0), 0.01f);
  126.         bool grounded4 = Physics.Raycast(corner4, new Vector3(0, -1, 0), 0.01f);
  127.  
  128.         // If any corner is grounded, the object is grounded
  129.         return (grounded1 || grounded2 || grounded3 || grounded4);
  130.     }
  131.  
  132.     void OnTriggerEnter(Collider collider)
  133.     {
  134.         // Check if we ran into a coin
  135.         if (collider.gameObject.tag == "Coin")
  136.         {
  137.             print("Grabbing coin..");
  138.  
  139.             // Increase score
  140.             GameManager.instance.IncreaseScore(1);
  141.  
  142.             //refresh the HUD
  143.             hud.Refresh();
  144.  
  145.             // Play coin collection sound
  146.             coinAudioSource.Play();
  147.  
  148.             // Destroy coin
  149.             Destroy(collider.gameObject);
  150.         }
  151.         else if (collider.gameObject.tag == "Enemy")
  152.         {
  153.             // Game over
  154.             print("game over");
  155.  
  156.             SceneManager.LoadScene("Game Over");
  157.         }
  158.         else if (collider.gameObject.tag == "Goal")
  159.         {
  160.             print("goal reached");
  161.  
  162.             // Increase level
  163.             GameManager.instance.IncreaseLevel();
  164.         }
  165.  
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement