Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PlayerController : MonoBehaviour
- {
- public float speed;
- private float moveInput;
- private float groundRadius = 0.2f;
- public Transform groundCheck;
- public LayerMask whatIsGround;
- private bool facingRight = true;
- private bool isGrounded = false;
- private Rigidbody2D rb;
- private Animator animator;
- void Start()
- {
- rb = GetComponent<Rigidbody2D>();
- animator = GetComponent<Animator>();
- }
- void FixedUpdate()
- {
- isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
- animator.SetBool ("Ground", isGrounded);
- animator.SetFloat("ySpeed", rb.velocity.y);
- if (!isGrounded)
- return;
- animator.SetFloat("Speed", Mathf.Abs(moveInput));
- }
- void Update()
- {
- moveInput = Input.GetAxis("Horizontal");
- rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
- if (facingRight == false && moveInput > 0)
- {
- Flip();
- }
- else if (facingRight == true && moveInput < 0)
- {
- Flip();
- }
- if (isGrounded && Input.GetKeyDown(KeyCode.Space))
- {
- animator.SetBool("Ground", false);
- rb.AddForce(new Vector2(0, 600));
- }
- }
- void Flip()
- {
- facingRight = !facingRight;
- Vector3 Scale = transform.localScale;
- Scale.x *= -1;
- transform.localScale = Scale;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment