Advertisement
Guest User

player

a guest
May 28th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Player : MonoBehaviour
  6. {
  7. Rigidbody2D rb;
  8. public float speed;
  9. public float jumpHeight;
  10. public Transform groundCheck;
  11. bool isGrounded;
  12. Animator anim;
  13. // Start is called before the first frame update
  14. void Start()
  15. {
  16. rb = GetComponent<Rigidbody2D>();
  17. anim = GetComponent<Animator>();
  18. }
  19.  
  20. // Update is called once per frame
  21. void Update()
  22. {
  23.  
  24. CheckGround();
  25. if (Input.GetAxis("Horizontal") == 0 && ( isGrounded))
  26. {
  27. anim.SetInteger("State",1);
  28. } else
  29. {
  30. Flip();
  31. if (isGrounded)
  32. anim.SetInteger("State", 2);
  33. }
  34. }
  35.  
  36. void FixedUpdate()
  37. {
  38. rb.velocity = new Vector2(Input.GetAxis("Horizontal") * speed, rb.velocity.y);
  39. if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
  40. rb.AddForce(transform.up * jumpHeight, ForceMode2D.Impulse);
  41.  
  42. }
  43.  
  44. void Flip()
  45. {
  46. if (Input.GetAxis("Horizontal") > 0)
  47. transform.localRotation = Quaternion.Euler(0, 0, 0);
  48. if (Input.GetAxis("Horizontal") < 0)
  49. transform.localRotation = Quaternion.Euler(0, 180, 0);
  50.  
  51. }
  52. void CheckGround()
  53. {
  54. Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheck.position, 0.2f);
  55. isGrounded = colliders.Length > 1;
  56. if (!isGrounded)
  57. anim.SetInteger("State", 3);
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement