Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class PlayerController : MonoBehaviour {
- private Vector2 moveDirection; // player's Vector
- [SerializeField] private Rigidbody2D rb2D; // player's rigidbody2D for unity physics
- private float Horizontal, Vertical; // This is for unity's built in Input..
- private float moveSpeed = 2.0f; // This is the player's movement speed.
- [SerializeField] private Animator _playerAnim; // players animator so, we can animate the player.
- /// <summary>
- /// Start is called on the frame when a script is enabled just before
- /// any of the Update methods is called the first time.
- /// </summary>
- private void Start()
- {
- rb2D = GetComponent<Rigidbody2D>(); // Get a reference to the rigidbody2D
- _playerAnim = GetComponent<Animator>(); // Get a reference to the player animator..
- }
- // Players movement method
- private void PlayerMovement() {
- Horizontal = Input.GetAxisRaw("Horizontal");
- Vertical = Input.GetAxisRaw("Vertical");
- moveDirection = new Vector2(Horizontal, Vertical) * moveSpeed;
- }
- // Players animator method..
- private void AnimatePlayer() {
- // We have 3 parameters in the Animator:
- // Two float values of: "X", "Y", and a Boolean "isMoving" which are being used in this method.
- _playerAnim.SetFloat("X", moveDirection.x);
- _playerAnim.SetFloat("Y", moveDirection.y);
- // check if the player is moving..
- if (moveDirection != Vector2.zero)
- {
- // If the player is moving.. set animation true..
- _playerAnim.SetBool("isMoving", true);
- }
- else {
- // otherwise set the animation false..
- _playerAnim.SetBool("isMoving", false);
- }
- }
- /// <summary>
- /// This function is called every fixed framerate frame, if the MonoBehaviour is enabled.
- /// </summary>
- private void FixedUpdate() {
- // Update the physics for our character's rigidbody2D
- rb2D.velocity = new Vector2(moveDirection.x, moveDirection.y);
- }
- /// <summary>
- /// Update is called every frame, if the MonoBehaviour is enabled.
- /// </summary>
- private void Update()
- {
- PlayerMovement(); // Calls to player's movement method to be updated every frame.
- AnimatePlayer(); // Calls to the players animation method to be updated every frame.
- // If you want teleporters in your game add this..
- // This does require the Teleport.cs file
- UseTeleportor();
- }
- // check if the entered the teleportor collider box..
- private void OnTriggerEnter2D(Collider2D other) {
- if (other.CompareTag("Teleportor")) {
- curTeleportor = other.gameObject;
- }
- }
- // check if the player exited the teleportor collider box..
- private void OnTriggerExit2D(Collider2D other) {
- if (other.CompareTag("Teleportor")) {
- if (other.gameObject == curTeleportor)
- curTeleportor = null;
- }
- }
- // A method for using the teleportor
- private void UseTeleportor() {
- // In project settings we have a input called "Interact"
- if (Input.GetButtonDown("Interact")) {
- if (curTeleportor != null) {
- // If we interact & teleportor isn't null, then transform the player to the other teleportor location..
- transform.position = curTeleportor.GetComponent<Teleport>().GetDestination().position;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement