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 PlayerController : MonoBehaviour {
- public float moveSpeed;
- private float moveVelocity;
- public float jumpHeight;
- public Transform groundCheck;
- public float groundCheckRadius;
- public LayerMask whatIsGround;
- private bool grounded;
- private bool doubleJumped;
- private Animator anim;
- void Start()
- {
- anim = GetComponent<Animator>();
- }
- void FixedUpdate()
- {
- grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
- }
- void Update () {
- if (grounded)
- {
- doubleJumped = false;
- }
- anim.SetBool("Grounded", grounded);
- if (Input.GetKeyDown(KeyCode.Space) && grounded)
- {
- Jump();
- }
- if (Input.GetKeyDown(KeyCode.Space) && !doubleJumped && !grounded)
- {
- Jump();
- doubleJumped = true;
- }
- moveVelocity = 0f;
- if (Input.GetKey(KeyCode.D))
- {
- moveVelocity = moveSpeed;
- }
- if (Input.GetKey(KeyCode.A))
- {
- moveVelocity = -moveSpeed;
- }
- if (Input.GetKey(KeyCode.S))
- {
- anim.SetBool("Holding S", true);
- }
- if (Input.GetKeyUp(KeyCode.S))
- {
- anim.SetBool("Holding S", false);
- }
- GetComponent<Rigidbody2D>().velocity = new Vector2(moveVelocity, GetComponent<Rigidbody2D>().velocity.y);
- anim.SetFloat("Speed", Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x));
- }
- public void Jump()
- {
- GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement