Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Player : MonoBehaviour
- {
- Rigidbody2D rb;
- public float speed;
- public float jumpHeight;
- public Transform groundCheck;
- bool isGrounded;
- Animator anim;
- // Start is called before the first frame update
- void Start()
- {
- rb = GetComponent<Rigidbody2D>();
- anim = GetComponent<Animator>();
- }
- // Update is called once per frame
- void Update()
- {
- CheckGround();
- if (Input.GetAxis("Horizontal") == 0 && ( isGrounded))
- {
- anim.SetInteger("State",1);
- } else
- {
- Flip();
- if (isGrounded)
- anim.SetInteger("State", 2);
- }
- }
- void FixedUpdate()
- {
- rb.velocity = new Vector2(Input.GetAxis("Horizontal") * speed, rb.velocity.y);
- if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
- rb.AddForce(transform.up * jumpHeight, ForceMode2D.Impulse);
- }
- void Flip()
- {
- if (Input.GetAxis("Horizontal") > 0)
- transform.localRotation = Quaternion.Euler(0, 0, 0);
- if (Input.GetAxis("Horizontal") < 0)
- transform.localRotation = Quaternion.Euler(0, 180, 0);
- }
- void CheckGround()
- {
- Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheck.position, 0.2f);
- isGrounded = colliders.Length > 1;
- if (!isGrounded)
- anim.SetInteger("State", 3);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement