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 {
- private Animator p_anim;
- private Rigidbody2D p_rb;
- private SpriteRenderer SpriteFlip;
- bool p_walk ;
- public float jump;
- public float speed;
- private float vertical;
- private float horizontal;
- bool grounded = false;
- void Start(){
- SpriteFlip = GetComponent<SpriteRenderer>();
- p_anim = GetComponent<Animator>();
- p_rb = GetComponent<Rigidbody2D>();
- p_walk = false;
- }
- void FixedUpdate () {
- transform.rotation = Quaternion.identity;
- horizontal = 0;
- if (Input.GetKey(KeyCode.D)) {
- SpriteFlip.flipX = false;
- horizontal = 1;
- p_walk = true;
- }
- if (Input.GetKey(KeyCode.Q)) {
- SpriteFlip.flipX = true;
- horizontal = -1;
- p_walk = true;
- }
- if (Input.GetKeyUp(KeyCode.D) || Input.GetKeyUp(KeyCode.Q)) {
- p_walk = false;
- horizontal = 0;
- }
- if (p_walk == true) {
- p_anim.SetBool ("walk", true); }
- if (p_walk == false) {
- p_anim.SetBool ("walk", false); }
- if (Input.GetKeyDown (KeyCode.Space) && grounded == true) {
- vertical = 1;
- }else{
- vertical = 0;
- }
- p_rb.MovePosition(p_rb.position + new Vector2(vertical, horizontal) * Time.fixedDeltaTime * speed);
- }
- void OnTriggerEnter2D() {
- grounded = true;
- }
- void OnTriggerExit2D() {
- grounded = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement