Advertisement
Guest User

Untitled

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