Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMove : MonoBehaviour
  6. {
  7. Animator animator;
  8. Rigidbody2D rb2d;
  9. SpriteRenderer spriteRenderer;
  10.  
  11. [SerializeField]
  12. Animator animations;
  13.  
  14. bool isGrounded;
  15. bool isRunningLeft;
  16. bool isRunningRight;
  17. bool inAir;
  18. bool isDuck;
  19. bool isUp;
  20. bool isIdle;
  21.  
  22. [SerializeField]
  23. Transform groundCheck;
  24.  
  25.  
  26. // Start is called before the first frame update
  27. void Start()
  28. {
  29. animator = GetComponent<Animator>();
  30. rb2d = GetComponent<Rigidbody2D>();
  31. spriteRenderer = GetComponent<SpriteRenderer>();
  32. }
  33.  
  34. // Update is called once per frame
  35. void Update()
  36. {
  37. animations.SetFloat("Speed", Mathf.Abs(rb2d.velocity.x));
  38. animations.SetFloat("JumpHeight", Mathf.Abs(rb2d.velocity.y));
  39. animations.SetBool("IsLookingUp", true);
  40. animations.SetBool("IsDucking", true);
  41. animations.SetBool("IsIdle", true);
  42.  
  43. if (Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground")))
  44. {
  45. isGrounded = true;
  46. }
  47. else
  48. {
  49. isGrounded = false;
  50. //animator.Play("Player_jump");
  51. }
  52.  
  53. if (Input.GetKey("right"))
  54. {
  55. rb2d.velocity = new Vector2(1, rb2d.velocity.y);
  56. //animator.Play("Player_walk");
  57. spriteRenderer.flipX = false;
  58. }
  59. else if (Input.GetKey("left"))
  60. {
  61. rb2d.velocity = new Vector2(-1, rb2d.velocity.y);
  62. //animator.Play("Player_walk");
  63. spriteRenderer.flipX = true;
  64. }
  65. else
  66. {
  67. rb2d.velocity = new Vector2(0, rb2d.velocity.y);
  68. if (isGrounded && !isRunningLeft && !isRunningRight && !inAir && !isDuck && !isUp)
  69. {
  70. isIdle = true;
  71. isGrounded = true;
  72. //animator.Play("Player_idle");
  73. }
  74. }
  75.  
  76. if (Input.GetKeyDown("space") && isGrounded)
  77. {
  78. inAir = true;
  79. rb2d.velocity = new Vector2(rb2d.velocity.x, 4);
  80. //animator.Play("Player_jump");
  81. }
  82.  
  83. if(Input.GetKey("down") && isGrounded && !isRunningLeft &&isRunningRight && !inAir && !isUp)
  84. {
  85. isDuck = true;
  86. //animator.Play("Player_duck");
  87. }
  88. else
  89. {
  90. isDuck = false;
  91. }
  92.  
  93. if(Input.GetKey("up") && isGrounded && !isRunningLeft && !isRunningRight && !inAir && !isDuck)
  94. {
  95. isUp = true;
  96. //animator.Play("Player_lookup");
  97. }
  98. else
  99. {
  100. isUp = false;
  101. }
  102.  
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement