Beg_Productions

Script de Player (Infinite Jumper)

Aug 22nd, 2016
371
0
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 J_PlayerController_Jumping : MonoBehaviour {
  5.    
  6.     public Transform camaraTrans;
  7.  
  8.     public GameManager gameManagerScript;
  9.  
  10.     [SerializeField]
  11.     float moveSpd;
  12.  
  13.     [SerializeField]
  14.     float jumpPower;
  15.  
  16.     Rigidbody2D myRigidbody2d;
  17.     SpriteRenderer mySpriteRenderer;
  18.  
  19.     bool isDead = false;
  20.  
  21.     void Awake(){
  22.  
  23.         myRigidbody2d = GetComponent<Rigidbody2D> ();
  24.         mySpriteRenderer = GetComponent<SpriteRenderer> ();
  25.  
  26.    
  27.     }
  28.  
  29.     void Update(){
  30.  
  31.         FlipCharacter ();
  32.         CheckIfDead ();
  33.     }
  34.  
  35.  
  36.     void FixUpdate () {
  37.  
  38.         Movement ();
  39.     }
  40.  
  41.     void OnCollisionEnter2D(Collision2D col) {
  42.  
  43.         if (col.gameObject.CompareTag ("Jumpable")) {
  44.             myRigidbody2d.velocity = new Vector2 (myRigidbody2d.velocity.x, 0f);
  45.             myRigidbody2d.AddForce (Vector2.up * jumpPower);
  46.         }
  47.  
  48.     }
  49.  
  50.     void Movement(){
  51.         Vector3 pos = myRigidbody2d.position;
  52.         myRigidbody2d.AddForce (Vector2.right * Input.GetAxis ("Horizontal") * moveSpd);
  53.         pos.x = Mathf.Clamp (pos.x, -2f, 2f);
  54.         myRigidbody2d.position = pos;
  55.  
  56.  
  57.     }
  58.  
  59.     void FlipCharacter(){
  60.  
  61.         if (Input.GetAxisRaw ("Horizontal") < 0)
  62.             mySpriteRenderer.flipX = true;
  63.         else if (Input.GetAxisRaw ("Horizontal") > 0)
  64.             mySpriteRenderer.flipX = false;
  65.  
  66.     }
  67.  
  68.     void CheckIfDead(){
  69.  
  70.         if (transform.position.y <= camaraTrans.position.y - 6f && !isDead) {
  71.             //Player Mati
  72.             isDead = true;
  73.             gameManagerScript.GameOver ();
  74.         }
  75.            
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment