Advertisement
raphael76280

Gab 2

Jan 30th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.43 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerController : MonoBehaviour {
  6.  
  7.  
  8.     private Animator p_anim;
  9.     private Rigidbody2D p_rb;
  10.  
  11.     private SpriteRenderer SpriteFlip;
  12.     bool p_walk ;
  13.  
  14.     public float jump;
  15.     public float speed;
  16.  
  17.     private float vertical;
  18.     private float horizontal;
  19.  
  20.     bool grounded = false;
  21.  
  22.  
  23.  
  24.     void Start(){
  25.  
  26.         SpriteFlip = GetComponent<SpriteRenderer>();
  27.         p_anim = GetComponent<Animator>();
  28.         p_rb = GetComponent<Rigidbody2D>();
  29.         p_walk = false;
  30.     }
  31.  
  32.     void FixedUpdate () {
  33.  
  34.         transform.rotation = Quaternion.identity;
  35.  
  36.        
  37.         horizontal = 0;
  38.  
  39.         if (Input.GetKey(KeyCode.D)) {
  40.  
  41.             SpriteFlip.flipX = false;
  42.             horizontal = 1;
  43.             p_walk = true;
  44.         }
  45.            
  46.         if (Input.GetKey(KeyCode.Q)) {
  47.  
  48.             SpriteFlip.flipX = true;
  49.             horizontal = -1;
  50.             p_walk = true;
  51.         }
  52.            
  53.         if (Input.GetKeyUp(KeyCode.D) || Input.GetKeyUp(KeyCode.Q)) {
  54.             p_walk = false;
  55.             horizontal = 0;
  56.         }
  57.  
  58.         if (p_walk == true) {
  59.             p_anim.SetBool ("walk", true); }
  60.  
  61.  
  62.         if (p_walk == false) {
  63.             p_anim.SetBool ("walk", false); }
  64.  
  65.         if (Input.GetKeyDown (KeyCode.Space) && grounded == true) {
  66.             vertical = 1;
  67.         }else{
  68.             vertical = 0;
  69.         }
  70.  
  71.         p_rb.MovePosition(p_rb.position + new Vector2(vertical, horizontal) * Time.fixedDeltaTime * speed);
  72.     }
  73.  
  74.     void OnTriggerEnter2D() {
  75.         grounded = true;
  76.     }
  77.        
  78.     void OnTriggerExit2D() {
  79.         grounded = false;
  80.     }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement