SHARE
TWEET

Untitled

a guest May 21st, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerController : MonoBehaviour
  6. {
  7.  
  8.     //Move speed of player
  9.     public float moveSpeed;
  10.  
  11.     //Animator variable, used to trigger animations
  12.     private Animator anim;
  13.     private Rigidbody2D myRigidbody;
  14.  
  15.     //Movement related stuff, playerMoving to indicate is player moving, lastMove to indicate when player last moved
  16.     public bool playerMoving;
  17.     private Vector2 lastMove;
  18.  
  19.     private static bool playerExists;
  20.  
  21.     public bool canMove;
  22.     // Start is called before the first frame update
  23.     void Start()
  24.     {
  25.         //Get the animator for the script
  26.         anim = GetComponent<Animator>();
  27.         myRigidbody = GetComponent<Rigidbody2D>();
  28.  
  29.  
  30.         //Prevent multiple players when scene hopping
  31.         if (!playerExists)
  32.         {
  33.             playerExists = true;
  34.             DontDestroyOnLoad(transform.gameObject);
  35.         }
  36.         else
  37.         {
  38.             Destroy(gameObject);
  39.         }
  40.  
  41.     }
  42.  
  43.     // Update is called once per frame
  44.     void Update()
  45.     {
  46.         if (canMove && playerMoving)
  47.         {
  48.             //Trigger animations to face on the XY axis
  49.             anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
  50.             anim.SetFloat("MoveY", Input.GetAxisRaw("Vertical"));
  51.         }
  52.  
  53.         if (!canMove)
  54.         {
  55.             myRigidbody.velocity = Vector2.zero;
  56.             return;
  57.         }
  58.  
  59.         //Put playermoving to false so no animations go off other than idle
  60.         playerMoving = false;
  61.  
  62.         if (canMove)
  63.         {
  64.             //Script to make the game understand WASD + KEYS
  65.             if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
  66.             {
  67.                 //Script to make the game understand movement * moveSpeed * time.deltatime to make accurate frame moving
  68.                 //transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
  69.                 myRigidbody.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * moveSpeed, myRigidbody.velocity.y);
  70.  
  71.                 //Make sure the variable is true to trigger animation
  72.                 playerMoving = true;
  73.  
  74.                 //Make the game remember your last movement direction
  75.                 lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
  76.             }
  77.  
  78.             //Repeat script for Y axis
  79.             if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
  80.             {
  81.                 //transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
  82.                 myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, Input.GetAxisRaw("Vertical") * moveSpeed);
  83.                 playerMoving = true;
  84.                 lastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
  85.             }
  86.  
  87.             if (Input.GetAxisRaw("Horizontal") < 0.5f && Input.GetAxisRaw("Horizontal") > -0.5f)
  88.             {
  89.                 myRigidbody.velocity = new Vector2(0f, myRigidbody.velocity.y);
  90.             }
  91.  
  92.             if (Input.GetAxisRaw("Vertical") < 0.5f && Input.GetAxisRaw("Vertical") > -0.5f)
  93.             {
  94.                 myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, 0f);
  95.             }
  96.         }
  97.  
  98.         anim.SetBool("PlayerMoving", playerMoving);
  99.         anim.SetFloat("LastMoveX", lastMove.x);
  100.         anim.SetFloat("LastMoveY", lastMove.y);
  101.     }
  102. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top