Advertisement
Guest User

Untitled

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