Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.53 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Player : MonoBehaviour
  5. {
  6.  
  7.     private Rigidbody2D _myRigidbody;
  8.     public float speed;
  9.     private Animator _anim;
  10.     private bool _facingRight = true;
  11.     private bool _canJump = true;
  12.     public Transform startPos;
  13.     public Transform endPos;
  14.     public LayerMask groundLayer;
  15.     // Use this for initialization
  16.     void Start ()
  17.     {
  18.         _myRigidbody = this.rigidbody2D;
  19.         _anim = GetComponent<Animator> ();
  20.     }
  21.  
  22.     // Update is called once per frame
  23.     void Update ()
  24.     {
  25.             float move = Input.GetAxisRaw("Horizontal");
  26.             _anim.SetFloat ("speed", Mathf.Abs(move));
  27.  
  28.             _myRigidbody.velocity = new Vector2(move * speed, _myRigidbody.velocity.y);
  29.  
  30.         if (_facingRight == true && move < 0)
  31.         {
  32.                         _facingRight = false;
  33.                         transform.rotation = Quaternion.Euler(transform.rotation.x, 180,    transform.rotation.z);
  34.         }
  35.         else if (_facingRight == false && move > 0)
  36.         {
  37.                         _facingRight = true;
  38.                         transform.rotation = Quaternion.Euler(transform.rotation.x, 0, transform.rotation.z);
  39.                 }
  40.  
  41.         Debug.DrawLine (startPos.position, endPos.position);
  42.  
  43.  
  44.  
  45.         RaycastHit2D hitInfo = Physics2D.Linecast(startPos.position, endPos.position,groundLayer.value);
  46.  
  47.  
  48.         if (hitInfo.collider != null)
  49.  
  50.        
  51.         {
  52.             _anim.SetBool ("Jump", false);
  53.             _canJump = true;
  54.  
  55.         }
  56.  
  57.  
  58.  
  59.         if (Input.GetKeyDown (KeyCode.Space) && _canJump == true)
  60.  
  61.  
  62.         {  
  63.             _anim.SetBool ("Jump", true);
  64.             _canJump = false;
  65.             _myRigidbody.velocity = new Vector2 (_myRigidbody.velocity.x, 5);
  66.  
  67.         }
  68.  
  69.  
  70.  
  71.     }
  72.        
  73.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement