Advertisement
Guest User

Untitled

a guest
May 21st, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  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", myRigidbody.velocity.y);
  50. anim.SetFloat("MoveY", myRigidbody.velocity.y);
  51. }
  52. else
  53. {
  54. myRigidbody.velocity = Vector2.zero;
  55. }
  56.  
  57. if (!canMove)
  58. {
  59. myRigidbody.velocity = Vector2.zero;
  60. return;
  61. }
  62.  
  63. //Put playermoving to false so no animations go off other than idle
  64. playerMoving = false;
  65.  
  66. if (canMove)
  67. {
  68. //Script to make the game understand WASD + KEYS
  69. if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
  70. {
  71. //Script to make the game understand movement * moveSpeed * time.deltatime to make accurate frame moving
  72. //transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
  73. myRigidbody.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * moveSpeed, myRigidbody.velocity.y);
  74.  
  75. //Make sure the variable is true to trigger animation
  76. playerMoving = true;
  77.  
  78. //Make the game remember your last movement direction
  79. lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
  80. }
  81.  
  82. //Repeat script for Y axis
  83. if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
  84. {
  85. //transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
  86. myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, Input.GetAxisRaw("Vertical") * moveSpeed);
  87. playerMoving = true;
  88. lastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
  89. }
  90.  
  91. if (Input.GetAxisRaw("Horizontal") < 0.5f && Input.GetAxisRaw("Horizontal") > -0.5f)
  92. {
  93. myRigidbody.velocity = new Vector2(0f, myRigidbody.velocity.y);
  94. }
  95.  
  96. if (Input.GetAxisRaw("Vertical") < 0.5f && Input.GetAxisRaw("Vertical") > -0.5f)
  97. {
  98. myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, 0f);
  99. }
  100. }
  101. else
  102. {
  103. playerMoving = false;
  104. }
  105.  
  106. anim.SetBool("PlayerMoving", playerMoving);
  107. anim.SetFloat("LastMoveX", lastMove.x);
  108. anim.SetFloat("LastMoveY", lastMove.y);
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement