Guest User

Untitled

a guest
Jan 30th, 2015
241
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlayerController : MonoBehaviour {
  5.  
  6.  
  7.     Animator anim;
  8.  
  9.     public float maxSpeed = 10f;
  10.  
  11.     bool facingRight = true;
  12.  
  13.     bool grounded = false;
  14.  
  15.     bool hitWallLeft;
  16.     bool hitWallRight;
  17.    
  18.     public Transform groundCheck;
  19.  
  20.     float groundRadius = 0.2f;
  21.  
  22.     public LayerMask whatIsGround;
  23.  
  24.     public float jumpForce = 700;
  25.  
  26.  
  27.     void Start()
  28.     {
  29.         anim = GetComponent<Animator> ();
  30.     }
  31.  
  32.  
  33.     void Update()
  34.     {
  35.  
  36.         if (Input.GetKeyDown (KeyCode.Space) && grounded)
  37.         {
  38.             anim.SetBool("Ground", false);
  39.  
  40.             rigidbody2D.AddForce(new Vector2(0, jumpForce));
  41.         }
  42.     }
  43.  
  44.  
  45.     void OnCollisionEnter2D (Collision2D col)
  46.     {
  47.         if(col.gameObject.tag == "Ground")
  48.         {
  49.             //Debug.Log("HIT");
  50.         }
  51.     }
  52.  
  53.  
  54.     void isHittingWallLeft(bool value)
  55.     {
  56.         hitWallLeft = value;
  57.     }
  58.  
  59.     void isHittingWallRight(bool value)
  60.     {
  61.         hitWallRight = value;
  62.     }
  63.    
  64.    
  65.     void FixedUpdate()
  66.     {
  67.         grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
  68.  
  69.         anim.SetBool ("Ground", grounded);
  70.  
  71.         anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
  72.  
  73.         float move = Input.GetAxis ("Horizontal");
  74.  
  75.         anim.SetFloat ("Speed", Mathf.Abs (move));
  76.  
  77.  
  78.  
  79.         /*if (hitWallLeft && move > 0)
  80.         {
  81.             rigidbody2D.velocity = new Vector2 (0, rigidbody2D.velocity.y);
  82.         }
  83.         else if(hitWallLeft && move < 0)
  84.         {
  85.             rigidbody2D.velocity = new Vector2 (move, rigidbody2D.velocity.y);
  86.         }*/
  87.  
  88.         if (hitWallRight && move < 0)
  89.         {
  90.             rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
  91.         }
  92.         else if(hitWallRight && move > 0)
  93.         {
  94.             rigidbody2D.velocity = new Vector2 (0, rigidbody2D.velocity.y);
  95.         }
  96.         else if (!hitWallRight && move > 0)
  97.         {
  98.             rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
  99.         }
  100.  
  101.  
  102.         if (hitWallLeft && move > 0)
  103.         {
  104.             rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
  105.         }
  106.         else if(hitWallLeft && move < 0)
  107.         {
  108.             rigidbody2D.velocity = new Vector2 (0, rigidbody2D.velocity.y);
  109.         }
  110.         else if (!hitWallLeft && move < 0)
  111.         {
  112.             rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
  113.         }
  114.  
  115.  
  116.        
  117.         if (move > 0 && !facingRight)
  118.         {
  119.             Flip ();///FACE RIGHT
  120.         }
  121.         else if (move < 0 && facingRight)
  122.         {
  123.             Flip ();
  124.         }
  125.     }
  126.  
  127.     void Flip()
  128.     {
  129.         facingRight = !facingRight;
  130.  
  131.         Vector3 theScale = transform.localScale;
  132.         theScale.x *= -1;
  133.         transform.localScale = theScale;
  134.     }
  135. }
RAW Paste Data