Advertisement
Guest User

Untitled

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