Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.21 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityStandardAssets.CrossPlatformInput;
  5.  
  6. public class PlayerPhysics : MonoBehaviour {
  7.  
  8.     //Erstellte Variablen
  9.     Rigidbody2D rb;
  10.     float dirX;
  11.  
  12.     public float speed = 5f;
  13.     public float jumpHeight = 600f;
  14.     public float maxSpeed = 3;
  15.  
  16.     public bool grounded;
  17.  
  18.     bool facingRight = true;
  19.     Vector3 localScale;
  20.  
  21.     private Animator anim;
  22.  
  23.  
  24.  
  25.  
  26.     void Start () {
  27.  
  28.         localScale = transform.localScale;
  29.         rb = GetComponent<Rigidbody2D> ();
  30.  
  31.         //Animationen des Char.
  32.         anim = gameObject.GetComponent<Animator>();
  33.  
  34.     }
  35.    
  36.     void Update () {
  37.        
  38.         //Steuerung
  39.         dirX = CrossPlatformInputManager.GetAxis ("Horizontal");
  40.  
  41.         if (CrossPlatformInputManager.GetButtonDown ("Jump"))
  42.             Jump ();
  43.  
  44.         //Animationen auf Char. bezug
  45.         anim.SetBool("Grounded", grounded);
  46.         anim.SetFloat("Speed", Mathf.Abs(rb.velocity.x));
  47.  
  48.  
  49.         //Drehen der Texturen während des Movements
  50.         if(CrossPlatformInputManager.GetAxis("Horizontal") < -0.1f)
  51.         {
  52.             transform.localScale = new Vector3(-1, 1, 1);
  53.         }
  54.        
  55.         if (CrossPlatformInputManager.GetAxis("Horizontal") < 0.1f)
  56.         {
  57.             transform.localScale = new Vector3(1, 1, 1);
  58.         }
  59.  
  60.  
  61.  
  62.     }
  63.  
  64.     void FixedUpdate()
  65.     {
  66.         //Laufen
  67.         rb.velocity = new Vector2 (dirX * speed, rb.velocity.y);
  68.  
  69.         //Speed limit
  70.         if (rb.velocity.x > maxSpeed)
  71.         {
  72.             rb.velocity = new Vector2(maxSpeed, rb.velocity.y);
  73.         }
  74.  
  75.         if (rb.velocity.x < -maxSpeed)
  76.         {
  77.             rb.velocity = new Vector2(-maxSpeed, rb.velocity.y);
  78.         }
  79.  
  80.  
  81.     }
  82.  
  83.     void LateUpdate()
  84.     {
  85.         CheckWhereToFace ();
  86.     }
  87.  
  88.     void CheckWhereToFace()
  89.     {
  90.         if (dirX > 0)
  91.             facingRight = true;
  92.         else
  93.             if (dirX < 0)
  94.                 facingRight = false;
  95.         if (((facingRight) && (localScale.x < 0)) || ((!facingRight) && (localScale.x > 0)))
  96.             localScale.x *= -1;
  97.         transform.localScale = localScale;
  98.     }
  99.  
  100.     void Jump()
  101.     {
  102.         if (CrossPlatformInputManager.GetButtonDown("Jump") && grounded)
  103.         {
  104.             rb.AddForce(Vector2.up * jumpHeight);
  105.         }
  106.     }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement