Guest User

1231ыва

a guest
Feb 9th, 2021
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.52 KB | None | 0 0
  1. public class PlayerController : MonoBehaviour
  2. {
  3.  
  4.     public float speed;
  5.     private float moveInput;
  6.     private float groundRadius = 0.2f;
  7.  
  8.     public Transform groundCheck;
  9.     public LayerMask whatIsGround;
  10.    
  11.  
  12.     private bool facingRight = true;
  13.     private bool isGrounded = false;
  14.  
  15.     private Rigidbody2D rb;
  16.     private Animator animator;
  17.     void Start()
  18.     {
  19.         rb = GetComponent<Rigidbody2D>();
  20.         animator = GetComponent<Animator>();
  21.     }
  22.     void FixedUpdate()
  23.     {
  24.         isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
  25.         animator.SetBool ("Ground", isGrounded);
  26.         animator.SetFloat("ySpeed", rb.velocity.y);
  27.         if (!isGrounded)
  28.             return;
  29.  
  30.         animator.SetFloat("Speed", Mathf.Abs(moveInput));
  31.        
  32.     }
  33.     void Update()
  34.     {
  35.         moveInput = Input.GetAxis("Horizontal");
  36.         rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
  37.  
  38.         if (facingRight == false && moveInput > 0)
  39.         {
  40.             Flip();
  41.         }
  42.         else if (facingRight == true && moveInput < 0)
  43.         {
  44.             Flip();
  45.         }
  46.         if (isGrounded && Input.GetKeyDown(KeyCode.Space))
  47.         {
  48.             animator.SetBool("Ground", false);
  49.             rb.AddForce(new Vector2(0, 600));
  50.  
  51.         }
  52.     }
  53.     void Flip()
  54.     {
  55.         facingRight = !facingRight;
  56.         Vector3 Scale = transform.localScale;
  57.         Scale.x *= -1;
  58.         transform.localScale = Scale;
  59.     }
  60.  
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment