Advertisement
Guest User

Untitled

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