Advertisement
Guest User

Untitled

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