Advertisement
UnityCoder_Jay

PlayerController.cs

Jan 8th, 2023 (edited)
766
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.48 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlayerController : MonoBehaviour {
  5.  
  6.     private Vector2 moveDirection; // player's Vector
  7.     [SerializeField] private Rigidbody2D rb2D; // player's rigidbody2D for unity physics
  8.     private float Horizontal, Vertical; // This is for unity's built in Input..
  9.     private float moveSpeed = 2.0f; // This is the player's movement speed.
  10.     [SerializeField] private Animator _playerAnim; // players animator so, we can animate the player.
  11.  
  12.     /// <summary>
  13.     /// Start is called on the frame when a script is enabled just before
  14.     /// any of the Update methods is called the first time.
  15.     /// </summary>
  16.     private void Start()
  17.     {
  18.         rb2D = GetComponent<Rigidbody2D>(); // Get a reference to the rigidbody2D
  19.         _playerAnim = GetComponent<Animator>(); // Get a reference to the player animator..
  20.     }
  21.    
  22.     // Players movement method
  23.     private void PlayerMovement() {
  24.         Horizontal = Input.GetAxisRaw("Horizontal");
  25.         Vertical = Input.GetAxisRaw("Vertical");
  26.  
  27.         moveDirection = new Vector2(Horizontal, Vertical) * moveSpeed;
  28.     }
  29.  
  30.     // Players animator method..
  31.     private void AnimatePlayer() {
  32.         // We have 3 parameters in the Animator:
  33.         // Two float values of: "X", "Y", and a Boolean "isMoving" which are being used in this method.
  34.         _playerAnim.SetFloat("X", moveDirection.x);
  35.         _playerAnim.SetFloat("Y", moveDirection.y);
  36.        
  37.         // check if the player is moving..
  38.         if (moveDirection != Vector2.zero)
  39.         {
  40.             // If the player is moving.. set animation true..
  41.             _playerAnim.SetBool("isMoving", true);
  42.         }
  43.         else {
  44.             // otherwise set the animation false..
  45.             _playerAnim.SetBool("isMoving", false);
  46.         }
  47.     }
  48.  
  49.     /// <summary>
  50.     /// This function is called every fixed framerate frame, if the MonoBehaviour is enabled.
  51.     /// </summary>
  52.     private void FixedUpdate() {
  53.         // Update the physics for our character's rigidbody2D
  54.         rb2D.velocity = new Vector2(moveDirection.x, moveDirection.y);
  55.     }
  56.  
  57.     /// <summary>
  58.     /// Update is called every frame, if the MonoBehaviour is enabled.
  59.     /// </summary>
  60.     private void Update()
  61.     {
  62.         PlayerMovement(); // Calls to player's movement method to be updated every frame.
  63.         AnimatePlayer(); // Calls to the players animation method to be updated every frame.
  64.        
  65.         // If you want teleporters in your game add this..
  66.         // This does require the Teleport.cs file
  67.         UseTeleportor();
  68.     }
  69.        
  70.  
  71.     // check if the entered the teleportor collider box..
  72.     private void OnTriggerEnter2D(Collider2D other) {
  73.         if (other.CompareTag("Teleportor")) {
  74.             curTeleportor = other.gameObject;
  75.         }
  76.     }
  77.     // check if the player exited the teleportor collider box..
  78.     private void OnTriggerExit2D(Collider2D other) {
  79.         if (other.CompareTag("Teleportor")) {
  80.             if (other.gameObject == curTeleportor)
  81.             curTeleportor = null;
  82.         }
  83.     }
  84.  
  85.     // A method for using the teleportor
  86.     private void UseTeleportor() {
  87.         // In project settings we have a input called "Interact"
  88.         if (Input.GetButtonDown("Interact")) {
  89.             if (curTeleportor != null) {
  90.                 // If we interact & teleportor isn't null, then transform the player to the other teleportor location..
  91.                 transform.position = curTeleportor.GetComponent<Teleport>().GetDestination().position;
  92.             }
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement