Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 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.  
  18. // Start is called before the first frame update
  19. void Start()
  20. {
  21. animator = GetComponent<Animator>();
  22. rb2d = GetComponent<Rigidbody2D>();
  23. spriteRenderer = GetComponent<SpriteRenderer>();
  24.  
  25. }
  26.  
  27. // Update is called once per frame
  28. void Update()
  29. {
  30.  
  31. }
  32.  
  33. private void FixedUpdate()
  34. {
  35. if(Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground")))
  36. {
  37. isGrounded = true;
  38. }
  39. else
  40. {
  41. isGrounded = false;
  42. }
  43.  
  44. if (Input.GetKey("right"))
  45. {
  46. rb2d.velocity = new Vector2(2, rb2d.velocity.y);
  47.  
  48. if (isGrounded)
  49. animator.Play("Player_walk");
  50.  
  51. spriteRenderer.flipX = false;
  52. }
  53.  
  54. else if (Input.GetKey("left"))
  55. {
  56. rb2d.velocity = new Vector2(-2, rb2d.velocity.y);
  57.  
  58. if (isGrounded)
  59. animator.Play("Player_walk");
  60.  
  61. spriteRenderer.flipX = true;
  62. }
  63.  
  64. else
  65. {
  66. rb2d.velocity = new Vector2(0, rb2d.velocity.y);
  67. animator.Play("Player_idle");
  68. }
  69.  
  70. if (Input.GetKey("space") && isGrounded)
  71. {
  72. rb2d.velocity = new Vector2(rb2d.velocity.x, 4);
  73. animator.Play("Player_jump");
  74. }
  75.  
  76. }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement