Advertisement
Guest User

Untitled

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